autoResizeDimensions is not doing anything
Environment details
- OS: Ubuntu 18.04.3 LTS
- Node.js version: 12.10.0
- npm version: 6.11.3
googleapisversion: 43.0.0
Steps to reproduce
- Create a new stylesheet with some data in it.
- Perform a batch update to resize the column width to fit the content.
Code sample:
Before the batch update, I'm calling update() to add some data in the sheet and this method is working as expected. It adds data in the sheet properly.
sheets.spreadsheets.values.update(request)
.catch((err) => { throw new Error(err); });
However, the batchUpdate() does not resize the column width of each cell.
let batchRequest = {
spreadsheetId: sheetId,
resource: {
requests: [{
autoResizeDimensions: {
dimensions: {
dimension: 'COLUMNS'
}
}
}]
}
};
sheets.spreadsheets.batchUpdate(batchRequest)
.catch((err) => { throw new Error(err); });
It doesn't throw any errors, but it's not resizing the column width. Am I missing something?
Hi @whyseman! Great question, I don’t think the documentation makes the solution immediately clear.
It looks like you’re missing some elements of the request in the object under autoResizeDimensions.
Try replacing your dimensions object with the following:
"dimensions": {
"sheetId": 0, //keep in mind that this is the sheetId, not the spreadsheet Id. see more info on the distinction here https://developers.google.com/sheets/api/guides/concepts#sheet_id
"dimension": "COLUMNS",
"startIndex": 0,
"endIndex": 15 //this is an arbitrary number. endIndex is exclusive so make sure you bump it up one from what you want the end to be
}
Furthermore, make sure that your spreadsheetId (under BatchRequest) is the spreadsheet ID, and not the sheet ID (again, reference the guide I linked above).
You can see more info on the correct structure of the object here: https://developers.google.com/sheets/api/reference/rest/v4/DimensionRange
If this doesn’t work for you, you might have an issue with authentication - I can’t really see the rest of your code so not sure. Feel free to test your code with the API Explorer on the right-hand side of this page: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate)
Please feel free to reopen if this doesn’t do the trick for you.
Ok, thanks @sofisl