google-apps-scriptgoogle-docsgoogle-docs-api

updateTableRowStyle: Unallowed field: tableHeader - Error using Google Docs API


I was trying to "copy" several tables from a GDocs template to another document. Everything was working fine until I tried to "copy" some tables with table headers.

Initially, I sent the following request for this:

"updateTableRowStyle": {
            "tableStartLocation": {
                "segmentId": "",
                "index": 552
            },
            "rowIndices": [
                0
            ],
            "tableRowStyle": {
                "minRowHeight": {
                    "unit": "PT"
                },
                "tableHeader": true
            },
            "fields": "*"
        }

This copied the table, but without the table headers. So, I decided to modify the fields parameter to the following request:

"updateTableRowStyle": {
            "tableStartLocation": {
                "segmentId": "",
                "index": 552
            },
            "rowIndices": [
                0
            ],
            "tableRowStyle": {
                "minRowHeight": {
                    "unit": "PT"
                },
                "tableHeader": true
            },
            "fields": "minRowHeight,tableHeader"
        }

When I did this, I got the following error:

"updateTableRowStyle: Unallowed field: tableHeader"

I already tried with other parameters of TableRowStyle and had no issues.

I'm not sure if this is due to limitations or bugs in the API. Your kind help would be appreciated.


Solution

  • Without a complete example, including an example of the expected result, it is impossible to figure out why your script is not working; however, I can tell you that UpdateTableRowStyleRequest works fine when used on a batch request with "*" or "minRowHeight" as the value for the fields property. See below


    The following works for me.

    1. Create a new document and set the document name.
    2. Click Extensions > Apps Script
    3. Set the Apps Script project name
    4. Add the Advanced Docs Service (Google Docs API)
    5. Replace the default content in Code.gs by the code in the below section.

    In the dropdown from the Google Apps Script Editor toolbar,

    1. Select insertTable, then click Run. As this is the first time that a function is executed in the project, you will be required to authorize the script.
    2. Select setTableHeaderStyle, then click Run.

    Sample Code

    The following includes two functions. The first inserts a new table on top of the document, and the second sets the top row style.

    function insertTable() {
      const insertTableRequest = {
        insertTable: {
          rows: 2,
          columns: 3,
          location: {
            index: 1
          }
        }
      }
      Docs.Documents.batchUpdate({ requests: [insertTableRequest] }, DocumentApp.getActiveDocument().getId());
    }
    
    function setTableHeaderStyle() {
      const updateTableRowStyleRequest = {
        updateTableRowStyle: {
          "tableStartLocation": {
            index: 2
          },
          "rowIndices": [
            0
          ],
          "tableRowStyle": {
            "minRowHeight": {
              "magnitude": 100,
              "unit": "PT"
            },
            "tableHeader": true,
            "preventOverflow": false
          },
          "fields": "*"
        }
      }
        Docs.Documents.batchUpdate({ requests: [updateTableRowStyleRequest] }, DocumentApp.getActiveDocument().getId());
    }
    

    Note:

    I don't know what effects setting the property tableHeader value to true or false might have.

    1. To make a table row repeat at the start of every page, the request to be used is pinTableHeaderRowRequest.
    2. The request to be used is insertText to copy the table header's text. Please remember that you should have one request for each table cell to copy.

    Resources

    Sidenote

    If you want to copy the table, it might be better to use the Documents Service (Class DocumentApp) instead of the Advanced Documents Service (Docs API).

    Related