使用 Google Apps 脚本填充 Google 文档中的 table 个单元格
Populate table cells in Google document with Google Apps Script
我想将 5x5 table 添加到空文档中。最终我想在几页中的每一页上添加一个。文档很神秘,但我从 Tanaike 那里找到了这个很好的例子:
var resource = {
requests: [
{ insertTable: { rows: 2, columns: 2, location: { index: 1 } } },
{ insertText: { text: "B2", location: { index: 12 } } },
{ insertText: { text: "A2", location: { index: 10 } } },
{ insertText: { text: "B1", location: { index: 7 } } },
{ insertText: { text: "A1", location: { index: 5 } } }
]
};
Docs.Documents.batchUpdate(resource, doc.getId());
所以我尝试使用相同的模式扩展到 5 x 5 table,但出现以下错误:
错误 GoogleJsonResponseException:API 调用 docs.documents.batchUpdate 失败,出现错误:无效 requests[5].insertText:插入索引必须在现有段落的边界内。您仍然可以通过插入换行符来创建新段落。
var requests = []
requests.push ({ insertTable:
{ rows: 5, columns: 5,
location: { index: 1 } }
});
requests.push ( { insertText: { text: "E5", location: { index: 33 } } } ); // 2
requests.push ( { insertText: { text: "D5", location: { index: 31 } } } ); // 3
requests.push ( { insertText: { text: "C5", location: { index: 29 } } } ); // 4
requests.push ( { insertText: { text: "B5", location: { index: 27 } } } ); // 5
requests.push ( { insertText: { text: "A5", location: { index: 25 } } } ); // 6
requests.push ( { insertText: { text: "E4", location: { index: 28 } } } ); // 7
requests.push ( { insertText: { text: "D4", location: { index: 26 } } } ); // 8
requests.push ( { insertText: { text: "C4", location: { index: 24 } } } );
requests.push ( { insertText: { text: "B4", location: { index: 22 } } } );
requests.push ( { insertText: { text: "A4", location: { index: 20 } } } );
requests.push ( { insertText: { text: "E3", location: { index: 23 } } } );
requests.push ( { insertText: { text: "D3", location: { index: 21 } } } );
requests.push ( { insertText: { text: "C3", location: { index: 19 } } } );
requests.push ( { insertText: { text: "B3", location: { index: 17 } } } );
requests.push ( { insertText: { text: "A3", location: { index: 15 } } } );
requests.push ( { insertText: { text: "E2", location: { index: 18 } } } );
requests.push ( { insertText: { text: "D2", location: { index: 16 } } } );
requests.push ( { insertText: { text: "C2", location: { index: 14 } } } );
requests.push ( { insertText: { text: "B2", location: { index: 12 } } } );
requests.push ( { insertText: { text: "A2", location: { index: 10 } } } );
requests.push ( { insertText: { text: "E1", location: { index: 13 } } } );
requests.push ( { insertText: { text: "D1", location: { index: 11 } } } );
requests.push ( { insertText: { text: "C1", location: { index: 09 } } } );
requests.push ( { insertText: { text: "B1", location: { index: 07 } } } );
requests.push ( { insertText: { text: "A1", location: { index: 05 } } } );
if (requests.length > 0) {
response = Docs.Documents.batchUpdate(
{ 'requests': requests }, doc.getId());
}
appendParagraph(..) 和 appendTable() 都是文档正文的有效方法,所以我认为我不需要在段落内添加一个带有 table 的段落?
更多研究和田内池也在这里回答
我添加了 endOfSegmentLocation 并得到了以下信息
GoogleJsonResponseException:API 调用 docs.documents.batchUpdate 失败,出现错误:
收到无效的 JSON 负载。 'requests[0]' 处的未知名称“endOfSegmentLocation”:找不到字段。
var requests = []
requests.push ({ insertTable:
{ rows: 5, columns: 5,
location: { index: 1 } },
endOfSegmentLocation: { segmentId: "" }
});
requests.push ( { insertText: { text: "E5", location: { index: 33 } } } ); // 2
requests.push ( { insertText: { text: "D5", location: { index: 31 } } } ); // 3
requests.push ( { insertText: { text: "C5", location: { index: 29 } } } ); // 4
requests.push ( { insertText: { text: "B5", location: { index: 27 } } } ); // 5
requests.push ( { insertText: { text: "A5", location: { index: 25 } } } ); // 6
requests.push ( { insertText: { text: "E4", location: { index: 28 } } } ); // 7
requests.push ( { insertText: { text: "D4", location: { index: 26 } } } ); // 8
requests.push ( { insertText: { text: "C4", location: { index: 24 } } } );
requests.push ( { insertText: { text: "B4", location: { index: 22 } } } );
requests.push ( { insertText: { text: "A4", location: { index: 20 } } } );
requests.push ( { insertText: { text: "E3", location: { index: 23 } } } );
requests.push ( { insertText: { text: "D3", location: { index: 21 } } } );
requests.push ( { insertText: { text: "C3", location: { index: 19 } } } );
requests.push ( { insertText: { text: "B3", location: { index: 17 } } } );
requests.push ( { insertText: { text: "A3", location: { index: 15 } } } );
requests.push ( { insertText: { text: "E2", location: { index: 18 } } } );
requests.push ( { insertText: { text: "D2", location: { index: 16 } } } );
requests.push ( { insertText: { text: "C2", location: { index: 14 } } } );
requests.push ( { insertText: { text: "B2", location: { index: 12 } } } );
requests.push ( { insertText: { text: "A2", location: { index: 10 } } } );
requests.push ( { insertText: { text: "E1", location: { index: 13 } } } );
requests.push ( { insertText: { text: "D1", location: { index: 11 } } } );
requests.push ( { insertText: { text: "C1", location: { index: 09 } } } );
requests.push ( { insertText: { text: "B1", location: { index: 07 } } } );
requests.push ( { insertText: { text: "A1", location: { index: 05 } } } );
if (requests.length > 0) {
response = Docs.Documents.batchUpdate(
{ 'requests': requests }, doc.getId());
}
我可以在循环中反复使用它,然后是 appendPageBreak()。
https://developers.google.com/apps-script/reference/document/table
有效,但我仍然可能需要使用 API 来设置 table 的样式,我想知道我尝试的方法有什么问题。
function loadTable(body, inArr) {
var inArrIdx = 0;
var cellArr = [], rowArr = [];
for (var r = 0 ; r < 5 ; r++ ) {
for (var c = 0 ; c < 5 ; c++ ) {
rowArr.push( inArr[inArrIdx] );
inArrIdx++;
}
cellArr.push( rowArr );
rowArr = [];
}
console.log('cellArr: ', cellArr );
// Build a table from the array.
body.appendTable(cellArr);
}
The source 是排他性的,因为它只是在谈论一个2列的索引规则-table
For the row, the index is required to set every 5 index.
For the column, the index is required to set every 2 index.
概括一下:
- 第一个单元格的索引:
5
- 列
n
的索引: index
+ n-1
列的 text
的长度
- 行索引
n
: index
+ text
的长度 + 最后一个单元格的 1
(换行符)行 n
的(列)
requests.push ( { insertText: { text: "E2", location: { index: 24 } } } );
requests.push ( { insertText: { text: "D2", location: { index: 22 } } } );
requests.push ( { insertText: { text: "C2", location: { index: 20 } } } );
requests.push ( { insertText: { text: "B2", location: { index: 18 } } } );
requests.push ( { insertText: { text: "A2", location: { index: 16 } } } );
requests.push ( { insertText: { text: "E1", location: { index: 13 } } } );
requests.push ( { insertText: { text: "D1", location: { index: 11 } } } );
requests.push ( { insertText: { text: "C1", location: { index: 09 } } } );
requests.push ( { insertText: { text: "B1", location: { index: 07 } } } );
requests.push ( { insertText: { text: "A1", location: { index: 05 } } } );
我想将 5x5 table 添加到空文档中。最终我想在几页中的每一页上添加一个。文档很神秘,但我从 Tanaike 那里找到了这个很好的例子:
var resource = {
requests: [
{ insertTable: { rows: 2, columns: 2, location: { index: 1 } } },
{ insertText: { text: "B2", location: { index: 12 } } },
{ insertText: { text: "A2", location: { index: 10 } } },
{ insertText: { text: "B1", location: { index: 7 } } },
{ insertText: { text: "A1", location: { index: 5 } } }
]
};
Docs.Documents.batchUpdate(resource, doc.getId());
所以我尝试使用相同的模式扩展到 5 x 5 table,但出现以下错误: 错误 GoogleJsonResponseException:API 调用 docs.documents.batchUpdate 失败,出现错误:无效 requests[5].insertText:插入索引必须在现有段落的边界内。您仍然可以通过插入换行符来创建新段落。
var requests = []
requests.push ({ insertTable:
{ rows: 5, columns: 5,
location: { index: 1 } }
});
requests.push ( { insertText: { text: "E5", location: { index: 33 } } } ); // 2
requests.push ( { insertText: { text: "D5", location: { index: 31 } } } ); // 3
requests.push ( { insertText: { text: "C5", location: { index: 29 } } } ); // 4
requests.push ( { insertText: { text: "B5", location: { index: 27 } } } ); // 5
requests.push ( { insertText: { text: "A5", location: { index: 25 } } } ); // 6
requests.push ( { insertText: { text: "E4", location: { index: 28 } } } ); // 7
requests.push ( { insertText: { text: "D4", location: { index: 26 } } } ); // 8
requests.push ( { insertText: { text: "C4", location: { index: 24 } } } );
requests.push ( { insertText: { text: "B4", location: { index: 22 } } } );
requests.push ( { insertText: { text: "A4", location: { index: 20 } } } );
requests.push ( { insertText: { text: "E3", location: { index: 23 } } } );
requests.push ( { insertText: { text: "D3", location: { index: 21 } } } );
requests.push ( { insertText: { text: "C3", location: { index: 19 } } } );
requests.push ( { insertText: { text: "B3", location: { index: 17 } } } );
requests.push ( { insertText: { text: "A3", location: { index: 15 } } } );
requests.push ( { insertText: { text: "E2", location: { index: 18 } } } );
requests.push ( { insertText: { text: "D2", location: { index: 16 } } } );
requests.push ( { insertText: { text: "C2", location: { index: 14 } } } );
requests.push ( { insertText: { text: "B2", location: { index: 12 } } } );
requests.push ( { insertText: { text: "A2", location: { index: 10 } } } );
requests.push ( { insertText: { text: "E1", location: { index: 13 } } } );
requests.push ( { insertText: { text: "D1", location: { index: 11 } } } );
requests.push ( { insertText: { text: "C1", location: { index: 09 } } } );
requests.push ( { insertText: { text: "B1", location: { index: 07 } } } );
requests.push ( { insertText: { text: "A1", location: { index: 05 } } } );
if (requests.length > 0) {
response = Docs.Documents.batchUpdate(
{ 'requests': requests }, doc.getId());
}
appendParagraph(..) 和 appendTable() 都是文档正文的有效方法,所以我认为我不需要在段落内添加一个带有 table 的段落?
更多研究和田内池也在这里回答
var requests = []
requests.push ({ insertTable:
{ rows: 5, columns: 5,
location: { index: 1 } },
endOfSegmentLocation: { segmentId: "" }
});
requests.push ( { insertText: { text: "E5", location: { index: 33 } } } ); // 2
requests.push ( { insertText: { text: "D5", location: { index: 31 } } } ); // 3
requests.push ( { insertText: { text: "C5", location: { index: 29 } } } ); // 4
requests.push ( { insertText: { text: "B5", location: { index: 27 } } } ); // 5
requests.push ( { insertText: { text: "A5", location: { index: 25 } } } ); // 6
requests.push ( { insertText: { text: "E4", location: { index: 28 } } } ); // 7
requests.push ( { insertText: { text: "D4", location: { index: 26 } } } ); // 8
requests.push ( { insertText: { text: "C4", location: { index: 24 } } } );
requests.push ( { insertText: { text: "B4", location: { index: 22 } } } );
requests.push ( { insertText: { text: "A4", location: { index: 20 } } } );
requests.push ( { insertText: { text: "E3", location: { index: 23 } } } );
requests.push ( { insertText: { text: "D3", location: { index: 21 } } } );
requests.push ( { insertText: { text: "C3", location: { index: 19 } } } );
requests.push ( { insertText: { text: "B3", location: { index: 17 } } } );
requests.push ( { insertText: { text: "A3", location: { index: 15 } } } );
requests.push ( { insertText: { text: "E2", location: { index: 18 } } } );
requests.push ( { insertText: { text: "D2", location: { index: 16 } } } );
requests.push ( { insertText: { text: "C2", location: { index: 14 } } } );
requests.push ( { insertText: { text: "B2", location: { index: 12 } } } );
requests.push ( { insertText: { text: "A2", location: { index: 10 } } } );
requests.push ( { insertText: { text: "E1", location: { index: 13 } } } );
requests.push ( { insertText: { text: "D1", location: { index: 11 } } } );
requests.push ( { insertText: { text: "C1", location: { index: 09 } } } );
requests.push ( { insertText: { text: "B1", location: { index: 07 } } } );
requests.push ( { insertText: { text: "A1", location: { index: 05 } } } );
if (requests.length > 0) {
response = Docs.Documents.batchUpdate(
{ 'requests': requests }, doc.getId());
}
我可以在循环中反复使用它,然后是 appendPageBreak()。
https://developers.google.com/apps-script/reference/document/table
有效,但我仍然可能需要使用 API 来设置 table 的样式,我想知道我尝试的方法有什么问题。
function loadTable(body, inArr) {
var inArrIdx = 0;
var cellArr = [], rowArr = [];
for (var r = 0 ; r < 5 ; r++ ) {
for (var c = 0 ; c < 5 ; c++ ) {
rowArr.push( inArr[inArrIdx] );
inArrIdx++;
}
cellArr.push( rowArr );
rowArr = [];
}
console.log('cellArr: ', cellArr );
// Build a table from the array.
body.appendTable(cellArr);
}
The source 是排他性的,因为它只是在谈论一个2列的索引规则-table
For the row, the index is required to set every 5 index. For the column, the index is required to set every 2 index.
概括一下:
- 第一个单元格的索引:
5
- 列
n
的索引:index
+n-1
列的 - 行索引
n
:index
+text
的长度 + 最后一个单元格的1
(换行符)行n
的(列)
text
的长度
requests.push ( { insertText: { text: "E2", location: { index: 24 } } } );
requests.push ( { insertText: { text: "D2", location: { index: 22 } } } );
requests.push ( { insertText: { text: "C2", location: { index: 20 } } } );
requests.push ( { insertText: { text: "B2", location: { index: 18 } } } );
requests.push ( { insertText: { text: "A2", location: { index: 16 } } } );
requests.push ( { insertText: { text: "E1", location: { index: 13 } } } );
requests.push ( { insertText: { text: "D1", location: { index: 11 } } } );
requests.push ( { insertText: { text: "C1", location: { index: 09 } } } );
requests.push ( { insertText: { text: "B1", location: { index: 07 } } } );
requests.push ( { insertText: { text: "A1", location: { index: 05 } } } );