【问题标题】:In a Google Document how to set all rows of all tables height to match the cell content size?在 Google 文档中,如何设置所有表格高度的所有行以匹配单元格内容大小?
【发布时间】:2020-10-01 15:36:48
【问题描述】:

我在这里找到了这个问题的答案: 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: Unexpected identifier ...”并提及脚本中的行:

for each (var table in tables) { 

in the answer of that question 提供的示例文档中尝试时显示错误“TypeError: table.getNumRows is not a function”

我尝试了各种描述表格的改变,表格,不同的获取行的方法没有运气。

这几个月有什么想法出错了,如何修复代码?

【问题讨论】:

  • 确切的错误是什么?

标签: google-apps-script google-docs


【解决方案1】:

修改点:

  • 我认为您的问题是由于 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

参考资料:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    • 2014-03-17
    • 2023-01-29
    • 2012-11-20
    • 2012-08-29
    • 1970-01-01
    • 2013-11-24
    相关资源
    最近更新 更多