使用谷歌脚本删除文本背景(通过格式>段落>边框和底纹>重置手动完成)

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

https://docs.google.com/document/d/1VrkXwfR24nIkX9KEGquIqFgkqPAke6MDst_N7YgSOY0/edit?usp=sharing

这是一个示例文档 我想做的是自动按下流程:格式>段落>边框和阴影>重置

目标是重置文档白色背景,这显然是通过边框和阴影完成的

所以总而言之,我想: 1.创建代码 2.在谷歌应用程序脚本上运行代码并查看它是否有效 3.将其添加为附加内容让我们重点关注1-2

我尝试过:

我尝试过:

function resetParagraphFormatting() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  
  var paragraphs = body.getParagraphs();
  for (var i = 0; i < paragraphs.length; i++) {
    var paragraph = paragraphs[i];
    paragraph.setAttributes({
      "BORDER_WIDTH": 0,
      "BORDER_COLOR": null,
      "BACKGROUND_COLOR": null
    });
  }
}

我们在操作脚本中执行,但当我运行它时它不会影响文档。

google-apps-script google-docs google-docs-api
1个回答
0
投票

我认为在您使用 Google Docs API 的情况下,脚本可能很简单。因此,在这个答案中,我想建议使用 Google Docs API 来实现您的目标。示例脚本如下。

示例脚本:

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

function resetParagraphFormatting() {
  const docId = DocumentApp.getActiveDocument().getId();
  const obj = Docs.Documents.get(docId).body.content;
  Docs.Documents.batchUpdate({ requests: [{ updateParagraphStyle: { range: { startIndex: 1, endIndex: obj.pop().endIndex }, fields: "borderBetween,borderBottom,borderTop,borderLeft,borderRight,shading", paragraphStyle: {} } }] }, docId);
}

测试:

运行此脚本时,“边框和阴影”的属性将重置如下。

注:

  • 如果您还想重置段落的空格,请将
    spaceAbove,spaceBelow
    的字段包含到
    fields: "borderBetween,borderBottom,borderTop,borderLeft,borderRight,shading"
    中。

参考资料:

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