Google Apps Script, select a sheet - Sets values in first sheet tab - not a specific sheet tab

被刻印的时光 ゝ 提交于 2021-02-07 12:25:18

问题


The script provided seems to work, but it posts the data to the first sheet tab of the spreadsheet, when I'd like to have it post on a sheet entitled XML.

I've tried changing getActiveSheet() to getSheetByName("XML") and a number of other things but have had no luck. Any ideas?

function getData() {
  var queryString = Math.random();

  var cellFunction1 = '=IMPORTXML("http://www.resources-game.ch/exchange/kurseliste.xml?' + queryString + '","//RESOURCES_RATES/ITEM")';
  SpreadsheetApp.getActiveSheet().getRange('A1').setValue(cellFunction1);

}

回答1:


Change this line:

SpreadsheetApp.getActiveSheet().getRange('A1').setValue(cellFunction1);

to this:

SpreadsheetApp.getActiveSpreadsheet()
  .getSheetByName('XML').getRange('A1')
  .setValue(cellFunction1);

I added getActiveSpreadsheet() which is different than getActiveSheet(). There is also a getActive() method which gets a spreadsheet. So, both getActive() and getActiveSpreadsheet() do the same thing. They get the spreadsheet, not a sheet. I've confused those 3 methods before.

  • getActive() - gets spreadsheet
  • getActiveSpreadsheet() - gets spreadsheet
  • getActiveSheet() - gets sheet


来源:https://stackoverflow.com/questions/34032425/google-apps-script-select-a-sheet-sets-values-in-first-sheet-tab-not-a-spec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!