使用 Google 脚本编辑和删除文档中的表格
Use Google Script to Edit and Delete Tables in Document
紧接在下面的是创建示例文档并向其中插入三个 table 的函数。三个现有的 table 在日志中显示为 [Table、Table、Table],正确计数为 3。如何“访问”或“select" 这些 table 用于编辑?我想从某人那里收到 在 table 1 末尾添加一行所需的代码,然后 删除 table2。如果我明白这一点,我想我会得到我需要的。
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.
}
谢谢!
将空行附加到 table 1. 使用此:
function appendRow() {
var body = DocumentApp.openById("doc id").getBody();
var tables = body.getTables();
var firstTable = tables[0].appendTableRow();
firstTable.appendTableCell();
firstTable.appendTableCell();
}
要删除文档中的第 2 个 table。使用这个:
function deleteTable(){
var body = DocumentApp.openById("doc id").getBody();
var tables = body.getTables();
tables[1].removeFromParent();
}
注意:Table 对象在 getTables()
数组中的顺序基于 table 在您的文档中的位置(从上到下),0 是起始索引.
示例:
之前:
执行appendRow()后:
执行删除后Table():
参考文献:
紧接在下面的是创建示例文档并向其中插入三个 table 的函数。三个现有的 table 在日志中显示为 [Table、Table、Table],正确计数为 3。如何“访问”或“select" 这些 table 用于编辑?我想从某人那里收到 在 table 1 末尾添加一行所需的代码,然后 删除 table2。如果我明白这一点,我想我会得到我需要的。
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.
}
谢谢!
将空行附加到 table 1. 使用此:
function appendRow() {
var body = DocumentApp.openById("doc id").getBody();
var tables = body.getTables();
var firstTable = tables[0].appendTableRow();
firstTable.appendTableCell();
firstTable.appendTableCell();
}
要删除文档中的第 2 个 table。使用这个:
function deleteTable(){
var body = DocumentApp.openById("doc id").getBody();
var tables = body.getTables();
tables[1].removeFromParent();
}
注意:Table 对象在 getTables()
数组中的顺序基于 table 在您的文档中的位置(从上到下),0 是起始索引.
示例:
之前:
执行appendRow()后:
执行删除后Table():