View difference between Paste ID: keXKHbhC and CrV3qHwG
SHOW: | | - or go back to the newest paste.
1
function onOpen(event) {
2
    allMenu();
3
}
4
5
function allMenu(){
6
  var slideUi = SlidesApp.getUi();
7
  slideUi.createMenu('TEST SO')
8
      .addItem('Create Shape', 'createShape')
9
      .addToUi();
10
}
11
12
function onInstall(event) {
13
  onOpen(event);
14
}
15
16
//I am trying to convert Table cell in Equal to shape. but issue with shape height.
17
18
function createShape() {
19-
  
19+
20
    let selection = activePresentation.getSelection()
21
    let pageElements = selection.getPageElementRange().getPageElements()
22
    
23
    //get table Left, table Top of each table under the pageElement object
24
    var tableLeft = pageElements[0].asTable().getLeft(),
25
        tableTop = pageElements[0].asTable().getTop();
26
        
27
    var cellTop = tableTop;        
28
    var cellLeft = tableLeft;
29
    
30
    
31-
    //get the row height and coloumn width, column width
31+
    //iterate page elements
32-
    var rowHeight = pageElements[0].asTable().getRow(0).getMinimumHeight();
32+
    pageElements.forEach(function(item, index) {
33-
    var columnWidth = pageElements[0].asTable().getColumn(0).getWidth();
33+
34
        //get number of rows and columns
35-
    //get the internal text of cell
35+
        var numColumns = pageElements[index].asTable().getNumColumns();
36-
    var cellText = pageElements[0].asTable().getCell(0, 0).getText().asString()
36+
        var numRows = pageElements[index].asTable().getNumRows();
37
38-
    var shape_t = selection.getCurrentPage().insertShape(SlidesApp.ShapeType.RECTANGLE, cellLeft+200, cellTop, columnWidth, rowHeight);
38+
        //get table Left, table Top of each table under the pageElement object
39-
    shape_t.getText().setText(cellText);
39+
        var tableLeft = pageElements[index].asTable().getLeft(),
40
            tableTop = pageElements[index].asTable().getTop();
41
42
        var rowHeight = 0.0,
43
            cellTop = tableTop;
44
45
        //iterate rows 
46
        for (var ir = 0; ir < numRows; ir++) {
47
48
            //get the row height, row height possibly would be height of each cell in this row index.
49
            var cellLeft = tableLeft,
50
                cellWidth = 0,
51
                cellHeight = 0,
52
                columnWidth = 0;
53
              
54
            //iterate columns 
55
            for ( var ic = 0; ic < numColumns; ic++ ) { //column start
56
57
                //get the coloumn width, column width possibly would be width of each cell in this row index.
58
                columnWidth = pageElements[index].asTable().getColumn(ic).getWidth();
59
                
60
                //Imp points :
61
                //columnWidth would be width of cell.
62
                
63
                try {
64
                
65
                    var rowHeight = 0.0;
66
                    
67
                    var tableCell = pageElements[index].asTable().getCell(ir, ic);
68
                    
69
                    //Get cell color
70
                    let cellFill = tableCell.getFill().getSolidFill();
71
                    
72
                    var cellParaText = tableCell.getText().getParagraphs();
73
                    cellParaText.forEach(function(item, index) {
74
                    
75
                      var t = cellParaText[index].getRange();
76
                      
77
                      //i tried to applied Tanike's solution here
78
                      rowHeight = rowHeight + ( t.asString().split("\n").length - 1 ) * ( t.getTextStyle().getFontSize() / 72 * 96 )
79
                      
80
                      //rowHeight = rowHeight + cellParaText[index].getRange().getTextStyle().getFontSize();
81
                      
82
                      rowHeight = rowHeight + cellParaText[index].getRange().getParagraphStyle().getSpaceAbove()
83
                      rowHeight = rowHeight + cellParaText[index].getRange().getParagraphStyle().getSpaceBelow()
84
                      rowHeight = rowHeight + (cellParaText[index].getRange().getParagraphStyle().getLineSpacing()/100)
85
                    
86
                    });
87
                    
88
                    //Get cell text, text color and text background color
89
                    let cellText = tableCell.getText().asString()
90
                     
91
                    let cellTextColor = tableCell.getText().getTextStyle().getForegroundColor()
92
                    let cellTextBackground = tableCell.getText().getTextStyle().getBackgroundColor()
93
                    let cellTextStyle = tableCell.getText().getTextStyle()
94
                    let cellParagraph = tableCell.getText().getParagraphStyle()
95
                    /*Logger.log(tableCell.getText().getParagraphStyle().getSpaceAbove(), tableCell.getText().getParagraphStyle().getSpaceBelow(), 
96
                                tableCell.getText().getParagraphStyle().getSpacingMode(), tableCell.getText().getParagraphStyle().getParagraphAlignment(),
97
                                tableCell.getText().getParagraphStyle().getLineSpacing())*/
98
                    //insert the RECTANGLE shape 
99
                    let insertedShape = selection.getCurrentPage()
100
                                          .insertShape(SlidesApp.ShapeType.RECTANGLE, cellLeft+5, cellTop+5, columnWidth, rowHeight);
101
                    
102
                    insertedShape.getText().setText(cellText).getTextStyle()
103
                                  .setFontSize(cellTextStyle.getFontSize())
104
                                  .setFontFamilyAndWeight( cellTextStyle.getFontFamily(), cellTextStyle.getFontWeight())
105
                    
106
                    insertedShape.getText()
107
                                .getParagraphStyle()
108
                                .setLineSpacing(cellParagraph.getLineSpacing()/100)
109
                                .setSpaceAbove(cellParagraph.getSpaceAbove())
110
                                .setSpaceBelow(cellParagraph.getSpaceBelow())
111
                                .setSpacingMode(cellParagraph.getSpacingMode())
112
                                .setParagraphAlignment(cellParagraph.getParagraphAlignment())
113
                    
114
                    if (cellFill != null) {
115
                        insertedShape.getFill().setSolidFill(cellFill.getColor().asRgbColor().asHexString());
116
                    } else {
117
                       insertedShape.getFill().setTransparent();
118
                    }
119
120
                    if (cellTextColor.getColorType() == "RGB") {
121
                        insertedShape.getText().getTextStyle().setForegroundColor(cellTextColor.asRgbColor().asHexString())
122
                    }
123
124
                    if (cellTextBackground.getColorType() == "RGB") {
125
                        insertedShape.getText().getTextStyle().setBackgroundColor(cellTextBackground.asRgbColor().asHexString())
126
                    }
127
128
                } catch (Err) {}
129
                
130
                //update cell left with column width + 5 padding
131
                cellLeft = cellLeft + columnWidth + 5;
132
              
133
            } //column end
134
135
            //update cell top with row height + 5 padding
136
            cellTop = cellTop + rowHeight + 5;
137
        }
138
    });
139
140
   selection.getCurrentPage().getTables()[0].remove();
141
    
142
}
143