下面是一個創建示例檔案并向其中插入三個表的函式。三個現有表在日志中顯示為 [表、表、表],正確計數為 3。如何“訪問”或“選擇”這些表進行編輯?我想從某人那里收到在 table 1 末尾添加一行所需的代碼,然后是洗掉 table 2的代碼。如果我明白這一點,我想我會得到我需要的。
function create_edit_delete_table() {
// Create sample table and get id
var docId = DocumentApp.create('SAMPLE_DOCUMENT').getId();
// Get doc body
var body = DocumentApp.openById(docId).getBody();
// Create a two-dimensional array containing the cell contents.
var cells = [
['Row 1, Cell 1', 'Row 1, Cell 2'],
['Row 2, Cell 1', 'Row 2, Cell 2']
];
// Build three tables from the array and insert into document.
body.appendTable(cells);
body.appendTable(cells);
body.appendTable(cells);
var tables = DocumentApp.openById(docId).getBody().getTables();
var tables_ct = DocumentApp.openById(docId).getBody().getTables().push();
Logger.log(tables);
Logger.log(tables_ct);
//Looking for code to add a blank row to the end of first table.
//Looking for code to delete the second table.
}
謝謝!
uj5u.com熱心網友回復:
將空行附加到表 1。使用以下命令:
function appendRow() {
var body = DocumentApp.openById("doc id").getBody();
var tables = body.getTables();
var firstTable = tables[0].appendTableRow();
firstTable.appendTableCell();
firstTable.appendTableCell();
}
洗掉檔案中的第二個表。用這個:
function deleteTable(){
var body = DocumentApp.openById("doc id").getBody();
var tables = body.getTables();
tables[1].removeFromParent();
}
注意:getTables()陣列中表格物件的順序基于表格在您的檔案中的位置(從上到下),0 是起始索引。
例子:
前:

執行 appendRow() 后:

執行 deleteTable() 后:

參考:
- TableRow.appendTableCell()
- Table.removeFromParent()
- Table.appendTableRow()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/374838.html
