【问题标题】:How can you set table cell padding properties using the Google Slides rest API?如何使用 Google Slides rest API 设置表格单元格填充属性?
【发布时间】:2019-05-22 01:54:05
【问题描述】:

有没有办法通过 REST API 更改 Google 幻灯片表格的表格单元格填充属性?

updateTableCellProperties 似乎没有任何单元格填充属性

【问题讨论】:

  • 不幸的是,没有关于通过 REST API 使用 Google 幻灯片进行单元格填充的信息。设置表格单元格填充的唯一解决方案是通过Google Apps Script。
  • @jess 填充仅适用于文档,不适用于幻灯片
  • 似乎没有任何方法可以通过 v1 REST API 更新 Google 幻灯片中的单元格填充。

标签: google-slides-api


【解决方案1】:

可以设置幻灯片表格填充!!!,但唯一的方法是设置带有模板表格的模板幻灯片文档以及您想要的填充。然后在下面的代码中,您可以将模板表格(和关联的填充)复制到目标幻灯片文档中的表格实例,然后相应地修改表格内容。这在 2021 年 9 月 26 日确实有效。我到处都用这个!我希望你觉得这很有用。

// open template slide document where you have manually create one or more tables with ONE cell ONLY with your desired padding
var lTemplatePresentation = SlidesApp.openById("1hd4b_EqLNmUEBQYkYTDrdeGEQ0E4d2lXXG2ClFBKkuo");
var lTemplateSlide = lTemplatePresentation.getSlides()[0];
var lTemplateTable = lTemplateSlide.getTables()[0];

// use the templates tables alt text title or description if you need multiple tables with different cell padding
Logger.log("Alt Text Title/Description: "+lTemplateTable.getTitle()+"/"+lTemplateTable.getDescription());

// open target presentation
var lMyPresentation = SlidesApp.openById("1-KWFQXtIsUW6Oo2dmpF54PqJHbVl258ph9bGMN6ifhM");
var lMySlide = lMyPresentation.getSlides()[0];

// this is the magic bit..   
var lMyTable = lMySlide.insertTable(lTemplateTable);

// modify table as required using normal methods
lMyTable.insertColumn(0); // yes cell padding is propogated from adjacent cells
lMyTable.insertColumn(0);
lMyTable.insertRow(0);
lMyTable.insertRow(0);
lMyTable.insertRow(0);
lMyTable.setTop(10).setLeft(10);

// NB: there seems to be a restriction on setting table width/height after the fact, so I suggest your template table
// has more columns which are sizes as needed. You can then always add more rows to your target tables programmatically.
// lMyTable.setHeight(100); // Note: 26/09/2021 Exception: Updating the size of this page element type is not supported.
// lMyTable.setWidth(300);  // Note: 26/09/2021 Exception: Updating the size of this page element type is not supported.

// Add text and modify paragraph indents to truely test cell padding has copied from the template slides document
var lMyCellA1 = lMyTable.getCell(0, 0);
var lMyCellTextRange = lMyCellA1.getText();
lMyCellTextRange.setText("HELLO WORLD ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !"
+"! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !");
var lMyParagraphStyle = lMyCellTextRange.getParagraphStyle();
lMyParagraphStyle.setLineSpacing(100 /*percent*/).setIndentFirstLine(0).setIndentStart(0);
lMyParagraphStyle.setIndentEnd(0).setSpaceBelow(0).setSpaceAbove(0);  

// that's it - job done!
lMyPresentation.saveAndClose();
lTemplatePresentation.saveAndClose();

【讨论】:

    【解决方案2】:

    虽然不完全相同,但可以通过设置单元格文本段落缩进和间距来实现类似结果的解决方法。

    将pageElement作为表,行和列索引传递给以下sn-p:

    function formatTableCell(table, rowIndex, colIndex) {
      const isHeader = rowIndex == 0;
    
      let verticalPadding; // in points
      let horizontalPadding; // in points
    
      if (isHeader) {
        verticalPadding = 7.2;
        horizontalPadding = 7.2;
      } else {
        verticalPadding = 7.2;
        horizontalPadding = 3.6; 
      }
    
      const cell = table.getCell(rowIndex, colIndex);
      const text = cell.getText();
      const p = text.getParagraphStyle();
    
      p.setLineSpacing(100)
        .setIndentFirstLine(verticalPadding)
        .setIndentStart(verticalPadding)
        .setIndentEnd(verticalPadding)
        .setSpaceBelow(horizontalPadding)
        .setSpaceAbove(horizontalPadding);
    
      cell.setContentAlignment(SlidesApp.ContentAlignment.MIDDLE);
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      • 2013-05-01
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多