问题描述
我发现这里回答了这个问题: In a Google Document how to set a tables multiple rows' height to match the cell content size? 该代码正是我所需要的:
function fixCellSize() {
DocumentApp.getUi().alert("All row heights will be minimized to content height.");
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var tables = body.getTables();
for each (var table in tables) {
for (var i=0; i < table.getNumRows(); i++){
Logger.log("Fantasctic!");
table.getRow(i).setMinimumHeight(1);
}
}
}
但是它不再起作用了。 如果将其添加到文档中的脚本中,则在保存时会显示错误:“ SyntaxError:意外的标识符...”,并提及脚本中的这一行:
for each (var table in tables) {
在提供的示例文档in the answer of that question中尝试显示错误“ TypeError:table.getNumRows不是函数”
这几个月来有什么想法出错,以及如何修复代码?
解决方法
修改点:
-
我认为您的问题是由于V8运行时引起的。 Ref例如,当您禁用V8运行时并使用Rhino运行时时,我认为
SyntaxError: Unexpected identifier ...
的错误将被消除并且脚本将起作用。但是在当前阶段,我想建议使用v8运行时。 -
当我看到您共享的文档时,我还找到了以下脚本。
for (var table in tables) { for (var i=0; i < table.getNumRows(); i++){ Logger.log("Fantasctic!"); table.getRow(i).setMinimumHeight(1); } }
- 我认为在这种情况下,
table
是字符串类型的数字。这样,我认为发生了TypeError: table.getNumRows is not a function
的错误。
- 我认为在这种情况下,
当以上几点反映到您的脚本中时,它如下所示。
发件人:
for each (var table in tables) {
for (var i=0; i < table.getNumRows(); i++){
Logger.log("Fantasctic!");
table.getRow(i).setMinimumHeight(1);
}
}
和
for (var table in tables) {
for (var i=0; i < table.getNumRows(); i++){
Logger.log("Fantasctic!");
table.getRow(i).setMinimumHeight(1);
}
}
收件人:
tables.forEach(table => {
for (var i=0; i < table.getNumRows(); i++){
Logger.log("Fantasctic!");
table.getRow(i).setMinimumHeight(1);
}
});
或
tables.forEach(function(table) {
for (var i=0; i < table.getNumRows(); i++){
Logger.log("Fantasctic!");
table.getRow(i).setMinimumHeight(1);
}
});
注意:
- 在这种情况下,请确认是否在脚本编辑器中启用了V8运行时。 Ref