Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);

}
like image 767
Plan-B Avatar asked Dec 01 '15 23:12

Plan-B


1 Answers

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
like image 100
Alan Wells Avatar answered Oct 21 '22 09:10

Alan Wells