您知道在普通的Web DOM中如何创建HTML元素,然后再将其添加到DOM中吗?
我想做与Google Apps脚本中相同的操作,尤其是对于Google Docs。也就是说,我要创建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
键。)
我知道我可以创建一个临时文档,然后向其中添加一个临时表,然后再将其删除。
但这意味着脚本可能会死掉,用户将不得不自己手动清理此临时文档。
答案 0 :(得分:0)
您可以创建2D数组chrome.history.deleteUrl({ 'url': myUrl });
。然后根据需要对其进行修改。 here接受字符串数组。
或者,Body.appendTable(array)
返回一个表对象,您以后可以修改/移动它。
答案 1 :(得分: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文档也会自动附加一个空白段落。不幸的是,尝试删除它会导致错误Google Apps Script: Can't remove the last paragraph in a document section.
因此,使用insertTable(0)
可以确保直到/除非您以后决定appendTable(table)
为止,原始文档绝对没有更改,也就是说,如果脚本在此之前失败,则无需进行任何清理。