寻找使用Google Apps脚本或DOCS API在Google文档中的段落中添加边框的方法吗? [英] Looking for a way to add borders to a paragraph in a google doc using a Google Apps Script or perhaps the DOCS API?

查看:100
本文介绍了寻找使用Google Apps脚本或DOCS API在Google文档中的段落中添加边框的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以通过 Format 菜单> 段落样式> 边框和底纹为Google文档段落添加边框. >

结果如下:

但是,我还没有弄清楚如何使用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文档中添加边框.
  • 您想使用Node.js和Google Apps脚本实现这一目标.
  • 您已经能够使用Google Docs API获取和放置Google文档的值.

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

可以通过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"
      }
    }
  ]
}

模式1:

在这种模式下,使用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脚本.在这种情况下,可以直接使用上面的请求正文.

示例脚本:

在使用此脚本之前,请请在Advanced 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运行时.

参考文献:

如果我误解了你的问题,而这不是你想要的方向,我深表歉意.

It is possible to add a border to a google doc paragraph via the Format menu > Paragraph styles > Borders and shading.

The result looks like this:

However, I have not managed to work out how to do this using Google Apps Scripts?

I have consulted the documentation concerning setting attributes, however, borders do no appear. It seems it is possible to set them using sheets and slides - but that isn't the use-case I am after.

I used the DOCS API via node.js to download the sample JSON returned from a document containing a border that I needed.

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));
  })
}

The part of the JSON that represented the effect I wished to create looked like this.

{
  "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"
          }
      }
  }
}

I am wondering if I could somehow make use of that instead of using Google Scripts - but the DOCS API does not appear to support borders.

Would be grateful for any clues, hints or tips?

解决方案

  • You want to add the border in Google Document using Google Docs API.
  • You want to achieve this using Node.js and Google Apps Script.
  • You have already been able to get and put values for Google Document with Google Docs API.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

The border can be added by the batchUpdate method in Google Docs API. In this case, UpdateParagraphStyleRequest is used. For example, from the JSON object in your question, when the following parameter is used,

"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"
    }
}

The request body for the batchUpdate method is as follows. In this case, as the sample, the border is added to the top of Document using {"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"
      }
    }
  ]
}

Pattern 1:

In this pattern, Node.js is used.

Sample script:

When the following script is run, a border is added to the top of Google Document.

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);
  }
);

Pattern 2:

In this pattern, Google Apps Script is used. In this case, the above request body can be used without modifying.

Sample script:

Before you use this script, 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);

  • In this case, please enable V8 runtime.

References:

If I misunderstood your question and this was not the direction you want, I apologize.

这篇关于寻找使用Google Apps脚本或DOCS API在Google文档中的段落中添加边框的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆