renaming sheets with “full sheet” charts on them c

2019-07-11 13:59发布

问题:

Google sheets allows charts to be moved to their own sheet. Google scripts appears to be unable to rename these sheets though, without crashing the spreadsheet and forcing it to reload.

To see what I mean try this: 1) create new spreadsheet and put some data in it. 2) create any type of chart w/ the data. 3) edit the chart and select "Move To Own Sheet..." 4) create a script w/ the following code:

  function myFunction() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheets = ss.getSheets();
    sheets[1].setName('bob');
  }

5) run the script and the spreadsheet crashes with the message: "Unable to Load File" 6) reload the spreadsheet and notice that the sheet did get renamed.

Why does the spreadsheet crash? Is it possible to rename the sheet w/o crashing?

回答1:

Unfortunately, I couldn't find the clear reason of the crash. But I think that there is a workaround for avoiding the crash. So how about this workaround? In this workaround, it uses Sheets API. In order to use Sheets API, please enable Sheets API at Advanced Google Services and API console. About the how to enable them, please check here.

The sample script using Sheets API is as follows.

Sample script :

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var id = ss.getId();
  var sheetId = ss.getSheets()[1].getSheetId();
  var resource = {"requests":[{"updateSheetProperties":{"properties":{"sheetId":sheetId,"title":"bob"},"fields":"title"}}]};
  Sheets.Spreadsheets.batchUpdate(resource, id);
}

If this was not what you want, I'm sorry.