【问题标题】:Looking for a way to add borders to a paragraph in a google doc using a Google Apps Script or perhaps the DOCS API?正在寻找一种使用 Google Apps 脚本或 DOCS API 为 google 文档中的段落添加边框的方法?
【发布时间】:2020-02-26 12:38:07
【问题描述】:

可以通过格式菜单 > 段落样式 > 边框和底纹为 google 文档段落添加边框。。 p>

结果如下:

但是,我还没有弄清楚如何使用 Google Apps 脚本来做到这一点?

我已经咨询了documentation关于设置属性,但是没有出现边框。似乎可以使用表格和幻灯片来设置它们 - 但这不是我所追求的用例。

我通过 node.js 使用 DOCS API 下载了从包含我需要的边框的文档返回的示例 JSON。

function printDocTitle(auth) {
  const documentId = '###';
  const docs = google.docs({version: 'v1', auth});
  docs.documents.get({
    documentId: documentId,
  }, (err, res) => {
    if (err) return console.log('The API returned an error: ' + err);
    const doc = res.data;
    console.log(JSON.stringify(doc, null, 4));
  })
}

代表我希望创建的效果的 JSON 部分如下所示。

{
  "startIndex": 82,
  "endIndex": 83,
  "paragraph": {
      "elements": [
          {
              "startIndex": 82,
              "endIndex": 83,
              "textRun": {
                  "content": "\n",
                  "textStyle": {
                      "fontSize": {
                          "magnitude": 12,
                          "unit": "PT"
                      },
                      "baselineOffset": "NONE"
                  }
              }
          }
      ],
      "paragraphStyle": {
          "namedStyleType": "NORMAL_TEXT",
          "alignment": "END",
          "direction": "LEFT_TO_RIGHT",
          "borderBottom": {
              "color": {
                  "color": {
                      "rgbColor": {}
                  }
              },
              "width": {
                  "magnitude": 1.5,
                  "unit": "PT"
              },
              "padding": {
                  "magnitude": 1,
                  "unit": "PT"
              },
              "dashStyle": "SOLID"
          }
      }
  }
}

我想知道是否可以以某种方式利用它而不是使用 Google 脚本 - 但 DOCS API 似乎不支持边框。

如果有任何线索、提示或提示,将不胜感激?

【问题讨论】:

  • 您使用过DocumentApp.Attribute 枚举吗?检查我的答案是否适合你。
  • 我标记为重复,因为它与 docs api 中的 updateParagraphStyle 请求基本相同。
  • 大师您好——不一样的...他们是不同的属性?您可以在谷歌应用程序脚本中更新行距 - 但您不能添加我可以看到的边框?因此,这个问题的重点是您无法使用 Google Apps 脚本更新的属性(边框 - 不是行距)。而且它似乎不在 DOC API 中。除非你知道我不知道的东西——请你分享一下吗?非常感谢。

标签: javascript node.js google-apps-script google-docs google-docs-api


【解决方案1】:
  • 您想使用 Google Docs API 在 Google 文档中添加边框。
  • 您希望使用 Node.js 和 Google Apps 脚本来实现这一目标。
  • 您已经能够使用 Google Docs API 获取和放置 Google Document 的值。

如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。

可以通过 Google Docs API 中的 batchUpdate 方法添加边框。在这种情况下,使用UpdateParagraphStyleRequest。例如,从您问题中的 JSON 对象中,当使用以下参数时,

"paragraphStyle": {
    "namedStyleType": "NORMAL_TEXT",
    "alignment": "END",
    "direction": "LEFT_TO_RIGHT",
    "borderBottom": {
        "color": {"color": {"rgbColor": {}}},
        "width": {"magnitude": 1.5, "unit": "PT"},
        "padding": {"magnitude": 1, "unit": "PT"},
        "dashStyle": "SOLID"
    }
}

batchUpdate 方法的请求体如下。在这种情况下,作为示例,使用{"startIndex": 1, "endIndex": 2} 将边框添加到 Document 的顶部。

{
  "requests": [
    {
      "updateParagraphStyle": {
        "paragraphStyle": {
          "namedStyleType": "NORMAL_TEXT",
          "alignment": "END",
          "direction": "LEFT_TO_RIGHT",
          "borderBottom": {
            "width": {"magnitude": 1.5, "unit": "PT"},
            "padding": {"magnitude": 1, "unit": "PT"},
            "dashStyle": "SOLID"
          }
        },
        "range": {"startIndex": 1, "endIndex": 2},
        "fields": "namedStyleType,alignment,direction,borderBottom"
      }
    }
  ]
}

模式一:

在此模式中,使用的是 Node.js。

示例脚本:

运行以下脚本时,Google 文档顶部会添加一个边框。

const documentId = "###";  // Please set the Document ID.

const docs = google.docs({ version: "v1", auth });
const requests = [
  {
    updateParagraphStyle: {
      paragraphStyle: {
        namedStyleType: "NORMAL_TEXT",
        alignment: "END",
        direction: "LEFT_TO_RIGHT",
        borderBottom: {
          width: { magnitude: 1.5, unit: "PT" },
          padding: { magnitude: 1, unit: "PT" },
          dashStyle: "SOLID"
        }
      },
      range: { startIndex: 1, endIndex: 2 },
      fields: "namedStyleType,alignment,direction,borderBottom"
    }
  }
];
docs.documents.batchUpdate(
  {
    documentId: documentId,
    requestBody: { requests }
  },
  (err, res) => {
    if (err) {
      console.log(err);
      return;
    }
    console.log(res.data);
  }
);

模式 2:

在此模式中,使用了 Google Apps 脚本。这种情况下,上面的请求体无需修改即可使用。

示例脚本:

在你使用这个脚本之前,please enable Docs API at Advanced Google services.

const documentId = "###";  // Please set the Document ID.
const requests = [
  {
    updateParagraphStyle: {
      paragraphStyle: {
        namedStyleType: "NORMAL_TEXT",
        alignment: "END",
        direction: "LEFT_TO_RIGHT",
        borderBottom: {
          width: { magnitude: 1.5, unit: "PT" },
          padding: { magnitude: 1, unit: "PT" },
          dashStyle: "SOLID"
        }
      },
      range: { startIndex: 1, endIndex: 2 },
      fields: "namedStyleType,alignment,direction,borderBottom"
    }
  }
];
const res = Docs.Documents.batchUpdate({requests}, documentId);
console.log(res);
  • 在这种情况下,请启用 V8 运行时。

参考资料:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

【讨论】:

  • 这个答案实际上引出了另一个问题:stackoverflow.com/questions/60432342/…
  • @Heartful Dodger 感谢您的回复。我很高兴你的问题得到了解决。我看到了你的新问题。我注意到已经发布了答案。我认为它会解决您的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-19
  • 1970-01-01
  • 2022-10-02
  • 1970-01-01
  • 2015-11-29
  • 1970-01-01
相关资源
最近更新 更多