如何使用 Apps 脚本从 Google 幻灯片演示文稿中的 table 中删除空列

How can I delete empty columns from a table in a Google Slides presentation with Apps Script

我有一个 google 应用程序脚本,可将数据从 google sheet 复制到 google 幻灯片模板的副本中。数据被复制到幻灯片模板中的占位符 table 中,因此有时幻灯片的 table 中会有额外的空白行,具体取决于在 sheet.[=13 中输入的内容=]

有人知道使用 google 应用程序脚本从 google 幻灯片上的 table 删除额外行的方法吗?

我看到 this similar post for doing the same thing on a google docs table, but it doesn't look like the Presentation class 有一个在该答案中使用的 getTables() 方法。

谢谢!

建议

您可以尝试下面的示例 Google 幻灯片 bound script,它会检查幻灯片演示文稿中的每一行 table,然后删除所有检测到的空行:

示例脚本

function removeEmptyRow() {
  var presentation = SlidesApp.getActivePresentation().getSlides()[0]; //Get the first slide
  var table = presentation.getTables()[0]; //Get the first table on the slide

  for(var col=0; col <= table.getNumColumns()-1; col++){
    for(var row=0; row<=table.getNumRows()-1; row++){
      var cellValue = table.getCell(row,col).getText().asString();
      if(cellValue.length == 1){ //check if current row is emtpy
        Logger.log("Row #"+row+" in column #"+col+" is empty");
        table.getRow(row).remove(); //remove this row index on the table
      }
    }
  }
}

参考文献:

Class TableRow

Class Slides getTables()

示例结果

Sample Slide Presentation table

After running the script:

2 empty rows were removed from the table