Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function onOpen(event) {
- allMenu();
- }
- function allMenu(){
- var slideUi = SlidesApp.getUi();
- slideUi.createMenu('TEST SO')
- .addItem('Create Shape', 'createShape')
- .addToUi();
- }
- function onInstall(event) {
- onOpen(event);
- }
- //I am trying to convert Table cell in Equal to shape. but issue with shape height.
- function createShape() {
- let activePresentation = SlidesApp.getActivePresentation()
- let selection = activePresentation.getSelection()
- let pageElements = selection.getPageElementRange().getPageElements()
- //get table Left, table Top of each table under the pageElement object
- var tableLeft = pageElements[0].asTable().getLeft(),
- tableTop = pageElements[0].asTable().getTop();
- var cellTop = tableTop;
- var cellLeft = tableLeft;
- //iterate page elements
- pageElements.forEach(function(item, index) {
- //get number of rows and columns
- var numColumns = pageElements[index].asTable().getNumColumns();
- var numRows = pageElements[index].asTable().getNumRows();
- //get table Left, table Top of each table under the pageElement object
- var tableLeft = pageElements[index].asTable().getLeft(),
- tableTop = pageElements[index].asTable().getTop();
- var rowHeight = 0.0,
- cellTop = tableTop;
- //iterate rows
- for (var ir = 0; ir < numRows; ir++) {
- //get the row height, row height possibly would be height of each cell in this row index.
- var cellLeft = tableLeft,
- cellWidth = 0,
- cellHeight = 0,
- columnWidth = 0;
- //iterate columns
- for ( var ic = 0; ic < numColumns; ic++ ) { //column start
- //get the coloumn width, column width possibly would be width of each cell in this row index.
- columnWidth = pageElements[index].asTable().getColumn(ic).getWidth();
- //Imp points :
- //columnWidth would be width of cell.
- try {
- var rowHeight = 0.0;
- var tableCell = pageElements[index].asTable().getCell(ir, ic);
- //Get cell color
- let cellFill = tableCell.getFill().getSolidFill();
- var cellParaText = tableCell.getText().getParagraphs();
- cellParaText.forEach(function(item, index) {
- var t = cellParaText[index].getRange();
- //i tried to applied Tanike's solution here
- rowHeight = rowHeight + ( t.asString().split("\n").length - 1 ) * ( t.getTextStyle().getFontSize() / 72 * 96 )
- //rowHeight = rowHeight + cellParaText[index].getRange().getTextStyle().getFontSize();
- rowHeight = rowHeight + cellParaText[index].getRange().getParagraphStyle().getSpaceAbove()
- rowHeight = rowHeight + cellParaText[index].getRange().getParagraphStyle().getSpaceBelow()
- rowHeight = rowHeight + (cellParaText[index].getRange().getParagraphStyle().getLineSpacing()/100)
- });
- //Get cell text, text color and text background color
- let cellText = tableCell.getText().asString()
- let cellTextColor = tableCell.getText().getTextStyle().getForegroundColor()
- let cellTextBackground = tableCell.getText().getTextStyle().getBackgroundColor()
- let cellTextStyle = tableCell.getText().getTextStyle()
- let cellParagraph = tableCell.getText().getParagraphStyle()
- /*Logger.log(tableCell.getText().getParagraphStyle().getSpaceAbove(), tableCell.getText().getParagraphStyle().getSpaceBelow(),
- tableCell.getText().getParagraphStyle().getSpacingMode(), tableCell.getText().getParagraphStyle().getParagraphAlignment(),
- tableCell.getText().getParagraphStyle().getLineSpacing())*/
- //insert the RECTANGLE shape
- let insertedShape = selection.getCurrentPage()
- .insertShape(SlidesApp.ShapeType.RECTANGLE, cellLeft+5, cellTop+5, columnWidth, rowHeight);
- insertedShape.getText().setText(cellText).getTextStyle()
- .setFontSize(cellTextStyle.getFontSize())
- .setFontFamilyAndWeight( cellTextStyle.getFontFamily(), cellTextStyle.getFontWeight())
- insertedShape.getText()
- .getParagraphStyle()
- .setLineSpacing(cellParagraph.getLineSpacing()/100)
- .setSpaceAbove(cellParagraph.getSpaceAbove())
- .setSpaceBelow(cellParagraph.getSpaceBelow())
- .setSpacingMode(cellParagraph.getSpacingMode())
- .setParagraphAlignment(cellParagraph.getParagraphAlignment())
- if (cellFill != null) {
- insertedShape.getFill().setSolidFill(cellFill.getColor().asRgbColor().asHexString());
- } else {
- insertedShape.getFill().setTransparent();
- }
- if (cellTextColor.getColorType() == "RGB") {
- insertedShape.getText().getTextStyle().setForegroundColor(cellTextColor.asRgbColor().asHexString())
- }
- if (cellTextBackground.getColorType() == "RGB") {
- insertedShape.getText().getTextStyle().setBackgroundColor(cellTextBackground.asRgbColor().asHexString())
- }
- } catch (Err) {}
- //update cell left with column width + 5 padding
- cellLeft = cellLeft + columnWidth + 5;
- } //column end
- //update cell top with row height + 5 padding
- cellTop = cellTop + rowHeight + 5;
- }
- });
- selection.getCurrentPage().getTables()[0].remove();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement