使用 Google Doc 的 Google Apps 脚本关闭跨页面的表格行溢出

问题描述 投票:0回答:1

是否有 Google Apps 脚本属性或方法来关闭现有 Google 文档中表格的“允许行跨页溢出”行选项?

我有下面的缩写代码可以正确检索表格,但属性值无效。

var body = DocumentApp.getActiveDocument().getBody();
var tables = body.getTables();
  for (var i = 0; i < tables.length; i++) {
    var table = tables[i];
    var rows = table.getNumRows();
    for (var j = 0; j < rows; j++) {
      var row = table.getRow(j);
      row.setAttributes({ "allowBreakAcrossPages": false });
    }
  }
google-apps-script google-docs google-docs-api
1个回答
0
投票

我相信你的目标如下。

  • 您想取消选中表格属性的“允许行溢出跨页”。
  • 您想使用 Google Apps 脚本实现此目标。

不幸的是,目前阶段似乎没有办法用谷歌文档服务(DocumentApp)取消选中它。但是,幸运的是,当使用 Google Docs API 时,这是可以实现的。在这个答案中,我想建议使用 Google Docs API。示例脚本如下

示例脚本:

在使用此脚本之前,请在高级 Google 服务中启用 Google Docs API

还有,请设置你要使用的表索引。在此示例中,文档中的第一个表与

const table = body.getTables()[0];
一起使用。

function myFunction() {
  const doc = DocumentApp.getActiveDocument();
  const documentId = doc.getId();
  const body = doc.getBody();
  const table = body.getTables()[0]; // Please set the index of the table.
  const index = Docs.Documents.get(documentId).body.content[body.getChildIndex(table) + 1].startIndex;
  const requests = [{
    updateTableRowStyle: {
      tableRowStyle: { preventOverflow: true },
      tableStartLocation: { index },
      fields: "preventOverflow",
    }
  }];
  Docs.Documents.batchUpdate({ requests }, documentId);
}
  • 运行该脚本时,“允许行跨页溢出”复选框未被选中。如需查看,请将
    preventOverflow: true
    修改为
    preventOverflow: false

参考资料:

© www.soinside.com 2019 - 2024. All rights reserved.