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.
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.
In the dropdown from the Google Apps Script Editor toolbar,
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.setTableHeaderStyle
, then click Run.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.
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).