Looking for a way to add borders to a paragraph in a google doc using a Google Apps Script or perhaps the DOCS API?

余生颓废 提交于 2020-04-17 22:09:29

问题


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?


回答1:


  • 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:

  • Method: documents.batchUpdate
  • UpdateParagraphStyleRequest
  • Advanced Google services

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




回答2:


Using Google Apps Script:

  • Use the Paragraph.setAttributes method.
  • Define the attributes using only DocumentApp.Attribute Enumerators.

Example:

// Define a style with black border.
var borderStyle = {};
borderStyle[DocumentApp.Attribute.BORDER_COLOR] = '#000000';
borderStyle[DocumentApp.Attribute.BORDER_WIDTH] = 2;

// Insert paragraph with border.
var body = DocumentApp.getActiveDocument().getBody();
var paragraph = body.appendParagraph("My Paragraph");
paragraph.setAttributes(borderStyle);


来源:https://stackoverflow.com/questions/60414139/looking-for-a-way-to-add-borders-to-a-paragraph-in-a-google-doc-using-a-google-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!