创建一个表对象,后来追加它

问题描述 投票:0回答:2

你知道如何在正常的网页DOM,你可以创建一个HTML元素,后来将它添加到DOM?

我想要做的是,在谷歌Apps脚本,特别是对于谷歌文档的等价物。也就是说,我想创建Table对象,姑且称之为table,而无需使用Body.appendTable()。再后来我想与Body.appendTable(table)追加它 - 这确实他们的API中已经存在。

问题是,我没有看到任何方法像createTable()任何地方这一点。

我已经试过

只需用new也不管用,显然:

var table = new Table();
var table = new Document.Table();
var table = new GoogleAppsScript.Document.Table();

相应的结果是:

ReferenceError: "Table" is not defined.
ReferenceError: "Document" is not defined.
ReferenceError: "GoogleAppsScript" is not defined.

(我试过基于https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/google-apps-script/google-apps-script.document.d.ts最后一个。)

或者,也许它会工作,如果我知道的路径Table接口。这不是这两种,但:

var table = new DocumentApp.Document.Table();

......因为产生:

TypeError: Cannot read property "Table" from undefined.

(这是有道理的。DocumentApp没有Document键)。

动机:

我知道我可以创建一个临时文档,然后添加一个临时表给它,之后将其删除。

但是,这意味着任何地方的脚本可能会死,用户必须手动清理此临时文档本身。

google-apps-script google-docs-api
2个回答
0
投票

您可以创建一个二维数组var table =[[]]。如你所愿。然后修改它。 Body.appendTable(array)接受字符串数组。

另外,Body.appendTable()返回一个表对象,你可以修改/后移动。


0
投票

您可以临时添加一个空表,然后将其删除,并参考还是不错的,可用于以后追加:

  var body = DocumentApp.getActiveDocument().getBody();
  var table = body.insertTable(0);
  body.removeChild(table);
  // ...
  table.appendTableRow().appendTableCell('test');
  // ...
  body.appendTable(table);

特别是,该地址没有任何清理,如果脚本无论是在// ...部件的模具的问题。

此外,最初使用appendTable()或任何其变体,而不是insertTable(0)意味着谷歌文档会自动添加一个空白段落为好。不幸的是,试图将其删除导致错误,Google Apps Script: Can't remove the last paragraph in a document section.

因此,使用insertTable(0)确保有原来的文件绝对没有改变,直到/除非你决定以后到任何appendTable(table),即没有进行清理,如果脚本点之前失败。

© www.soinside.com 2019 - 2024. All rights reserved.