简体   繁体   English

应用程序脚本 - 删除谷歌文档中的特定表

[英]App script - Delete a specific table in google doc

I'm trying to make app script to create a google doc filled in function of some given answer, given in a spreadsheet.我正在尝试制作应用程序脚本来创建一个谷歌文档,其中填写 function 的一些给定答案,在电子表格中给出。 I've some problem to remove a specific table that is present in a google doc template that must to be copied and filled as told.我在删除 google doc 模板中存在的特定表格时遇到了一些问题,该表格必须按照说明进行复制和填写。

function myFunction() {
  var ModuloRaccoltaDati = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("MRD");
  var ColonnaDaCopiare = ModuloRaccoltaDati.getLastColumn(); 
  var NumErog = ModuloRaccoltaDati.getRange(15,ColonnaDaCopiare).getValue();
  var file = DriveApp.getFileById('1ubPramWI890GDkcEdIoQqGxuXR0kBo6O-nOn1_gtlII'); 
  var folder = DriveApp.getFolderById('1ce-bya5gwwn4TQdu7hDIVmjqDajTyYhg');
  var copy = file.makeCopy(NumeroDiImpianto, folder); 
  var doc = DocumentApp.openById(copy.getId()); 
  var body = doc.getBody();
  if (NumErog == 1) {
    var tableErog2 = body.getTables()[1];
    tableErog2.deletetable();
  }
}

All this creates the new doc...but doesn't work about the table removing.所有这一切都会创建新的文档......但对于删除表格不起作用。

The script is not deleting the table because you are using a non-existing method to do that ( deletetable() ).该脚本不会删除表,因为您正在使用不存在的方法来执行此操作( deletetable() )。

To remove a Table from a Document using Apps Script, I can see two easy options:要使用 Apps 脚本从文档中删除表格,我可以看到两个简单的选项:

So you should be able to get this done by changing this line:因此,您应该可以通过更改此行来完成此操作:

tableErog2.deletetable();

To this one:对此:

tableErog2.removeFromParent();

Or alternatively, to this one:或者,对于这个:

body.removeChild(tableErog2);

I hope this is of any help.我希望这有任何帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM