javascript - 正在寻找一种使用 Google Apps 脚本或 DOCS API 为 google 文档中的段落添加边框的方法?
问题描述
可以通过Format menu > Paragraph styles > Borders and shading为 google doc 段落添加边框。
结果如下所示:
但是,我还没有设法弄清楚如何使用 Google Apps 脚本来做到这一点?
我已经查阅了有关设置属性的文档,但是没有出现边框。似乎可以使用工作表和幻灯片来设置它们——但这不是我所追求的用例。
我通过 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 似乎不支持边框。
如果有任何线索、提示或提示,将不胜感激?
解决方案
- 您想使用 Google Docs API 在 Google Document 中添加边框。
- 您想使用 Node.js 和 Google Apps Script 来实现这一点。
- 您已经能够使用 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 方法的请求正文如下。在这种情况下,作为示例,边框被添加到 Document 的顶部,使用{"startIndex": 1, "endIndex": 2}
.
{
"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);
}
);
模式二:
在此模式中,使用了 Google Apps 脚本。在这种情况下,无需修改即可使用上述请求正文。
示例脚本:
在使用此脚本之前,请在高级 Google 服务中启用 Docs API。
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 运行时。
参考:
如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
推荐阅读
- r - Rstudio 中没有显示函数输出
- maven - 如何解决 liquibase 错误找不到基本路径“/db/changeLog-master.xml”
- python - Python Unittests 输出 - 是否可以在每个通过/失败行下打印出 unittests 中的打印语句?
- python-3.x - 熊猫根据行从一张纸复制到另一张纸
- linux - 仅限基本 bash:通过具有未知模式的主键插入/更新 .csv 文件和另一个 .csv 文件
- node.js - 如何在集合组查询中每个子集合只获取一个文档?
- r - Problem with post hoc comparison using Robust ANOVA in R
- css - angular issue in scss file
- conda - gfortran not running on OSX dyld: Library not loaded: @rpath/libgfortran.3.dylib
- c# - Walking main character by WSAD and rotation camera with mouse