Question

I'm trying to use HTTP Get in Google Spreadsheet, to be able to create a URL to a specific cell in a sheet.
I've been searching for how to do it, and I've tried using this way (the GET part), for example.

Here's the code I've put together:

function doGet(e){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Kunder");
  var row = e.parameter["row"];
  var col = e.parameter["col"];

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var range = sheet.getRange(row, col);
  SpreadsheetApp.setActiveSheet(sheet);
  SpreadsheetApp.setActiveRange(range);
}

And this is an example URL:

https://docs.google.com/spreadsheets/d/[spreadsheet ID]/edit?row=5&col=1

When I run the script I get this error:

TypeError: Cannot read property "parameter" from undefined.

I'm I doing something wrong? Or isn't it possible?

Thanks in advance,
Oskar

EDIT
This is my current code:

function doGet(e){
  var ss = SpreadsheetApp.openById("[id]");
  SpreadsheetApp.setActiveSpreadsheet(ss);
  var sheet = ss.getSheetByName("Kunder");
  var row = Number(e.parameter.row);
  var col = Number(e.parameter.col);

  var range = sheet.getRange(row, col);
  SpreadsheetApp.setActiveSheet(sheet);
  SpreadsheetApp.setActiveRange(range);
}

The code runs but finishes with

The script completed but did not return anything.

I want to open the spreadsheet and then set the active range from the GET parameters.

Was it helpful?

Solution

The url you show is not valid.

To use that kind of script you have to deploy the script as a webapp and use the resulting .exec url with parameters.

At the end it will look like this :

https://script.google.com/macros/s/AKfycbygwUwXXXXXXXXxJcKpBvsbflmnZ0Uqfu-JISWTZNvar32s3v_hl/exec?row=5&col=1

EDIT : there will be no activeSheet in that context, use openById or openByUrl instead. Also the values you get for row and columns are strings, you should make them integers and use a normal sheet.getRange(row,col) to select a range.

EDIT2 :

this url :

https://script.google.com/macros/s/AKfycbwZbGW6E483BUplvLjCjNCXKjjiRorqzR9lruSydogeuU-YIvID/exec?row=1&col=1

and this code :

function doGet(e){
  var ss = SpreadsheetApp.openById("1sbJXuEpL_88-u-Bm4QOCAM3SVddFwZKI0c1kxeRpcos");
  var sheet = ss.getSheetByName("Sheet1");
  var row = Number(e.parameter.row); 
  var col = Number(e.parameter.col); 
  var range = sheet.getRange(row, col);
  sheet.getRange(row, col).setValue('test ok')
}

writes in this sheet : see cell A1 or test on another row and column.

You'll indeed receive a message that nothing was returned and this is actually true ! we don't return anything in that code, this is just a demo ;-)


EDIT 3 : as mentioned in the comments, to get something returned by this function you have to resturn something...

Here is an example :

function doGet(e){
  var ss = SpreadsheetApp.openById("1sbJXuEpL_88-u-Bm4QOCAM3SVddFwZKI0c1kxeRpcos");
  var sheet = ss.getSheetByName("Sheet1");
  var row = Number(e.parameter.row); 
  var col = Number(e.parameter.col); 
  var range = sheet.getRange(row, col);
  var value = sheet.getRange(row, col).getValue();
  sheet.getRange(row, col).setValue('test ok');
  var stringValue = value==''?'empty':value;
  Logger.log(stringValue);
  return ContentService.createTextOutput(stringValue).setMimeType(ContentService.MimeType.TEXT);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top