1
votes

I'm looking for a way to get the border width of a Google Docs using the API?

I have setup a test document that has two 1-cell tables. First one with default borders and the second one with 0-width borders and a third one with 3 pt border.

https://docs.google.com/document/d/1KOiw6bmsNhdOGGr9OZDWHNHrEID5RZncyCRRFFSU_Ok/edit

I retrieve the document using the get request

I can't find any difference between the three tables in terms of border width.

According to the documentation TableCellStyle should have borderTop/Bottom/Left/Right values but I'm not sure what I need to do to have them included in the response.

All 3 tableCellStyle looks the same, like this:

              "tableCellStyle": {
                "rowSpan": 1,
                "columnSpan": 1,
                "backgroundColor": {},
                "paddingLeft": {
                  "magnitude": 5,
                  "unit": "PT"
                },
                "paddingRight": {
                  "magnitude": 5,
                  "unit": "PT"
                },
                "paddingTop": {
                  "magnitude": 5,
                  "unit": "PT"
                },
                "paddingBottom": {
                  "magnitude": 5,
                  "unit": "PT"
                },
                "contentAlignment": "TOP"
              }

Each table cell contain a paragraph with a the same paragraphstyle:

                      "borderLeft": {
                        "color": {},
                        "width": {
                          "unit": "PT"
                        },
                        "padding": {
                          "unit": "PT"
                        },
                        "dashStyle": "SOLID"
                      },

Update

I was setting the table border in the above example which is not represented in the API response. My assumption was that the table border was reflected in the cells in the response data since there was no property for the table border itself. Which is partly correct, see answers.

2
Can you provide a sample Document for replicating your situation? I would like to confirm it. Of course, please remove your personal information. - Tanaike
Thank you for replying. Unfortunately, I cannot replicate your situation from your replying. So I cannot think of the issue and solution. I apologize for this. But I noticed that an answer has already been posted. I think that it will resolve your issue. - Tanaike
@Tanaike I've updated the question with a shared docment. I don't get the differences as you get, I also tried modifying the document, from 0 to 3 pt border but the only change recorded in the response was the descriptive text I changed inside the cell. - hultqvist
Thank you for replying and adding more information. I could confirm about your situation. At Google API, when the value is 0, there is the case that the property is not shown. For example, this can be seen at Sheets API. Ref This might be the current specification. I think that about the border width of 0, this is the reason of "width": {"unit": "PT"},. About the border widths of 1 and 3, I could replicate your issue. I think that this might be a bug. Docs API is growing now. So this might be resolved in the future. - Tanaike
So as the current workaround, how about modifying the border color? When the border color is changed, the border widths of 1 and 3 can be included in the response value from Docs API. About this issue, I think that this might be related to above situation. Because the default border color is the black which has 0, 0, 0 for red, green and blue, respectively. By this, rgbColor of the border color has no properties. - Tanaike

2 Answers

2
votes

From the TableCellStyle documentation:

Inherited table cell styles are represented as unset fields in this message. A table cell style can inherit from the table's style.

In this case, TableCellStyle inherits the default borderLeft, borderRight, borderTop and borderBottom with the default style, the representation of which would look as follows:

"borderTop": {
  "color": {
    "color": {
      "rgbColor": {}
    }
  },
  "width": {
    "magnitude": 1,
    "unit": "PT"
  },
  "dashStyle": "SOLID"
}

Essentially, if you are trying to obtain the borders width, you would have to firstly check whether that border field exists in your data. If it doesn't, you would set the default value to it (1 PT). As an example, using JavaScript with its ternary operator:

var borderTopMagnitude = tableCellStyle.borderTop ? tableCellStyle.borderTop.width.magnitude : 1;
var borderBottomMagnitude = tableCellStyle.borderBottom ? tableCellStyle.borderBottom.width.magnitude : 1;
var borderLeftMagnitude = tableCellStyle.borderLeft ? tableCellStyle.borderLeft.width.magnitude : 1;
var borderRightMagnitude = tableCellStyle.borderRight ? tableCellStyle.borderRight.width.magnitude : 1;
0
votes

Starting with a new table the following applies in order.

  1. Changing table border styles won't reflect in the API response.

  2. Changes in individual cells, or all cells at once, will be reflected in the API response.

  3. Once step 2 is performed, changes in table border style will now be reflected in the API response, but only for the cells that have been modified individually previously.