1

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.

Punchlinern
  • 714
  • 5
  • 17
  • 33
  • see edit please... my answer was about the context, not the code content. I added a few hints that should help you to make it work. – Serge insas Feb 15 '14 at 16:25
  • Thanks! Yeah, I got that and changed my code as you can see above. I'm not sure how to change a string into an integer. I've tried the Number() function and "var numCol = +col" but get "Cannot convert NaN to (class)." in both cases. – Punchlinern Feb 15 '14 at 16:51
  • try simply like that : var row = Number(e.parameter.row); var col = Number(e.parameter.col); – Serge insas Feb 15 '14 at 17:20
  • I did, I get "Cannot convert NaN to (class)." – Punchlinern Feb 15 '14 at 17:21
  • are you sure you removed the brackets and quotes ? – Serge insas Feb 15 '14 at 17:23
  • Yeah, I just copied your example. Copied from my file: var row = Number(e.parameter.row); var col = Number(e.parameter.col); – Punchlinern Feb 15 '14 at 17:25
  • The converting problem was a very stupid mistake by me, when I realized that it isn't enough to save the script to update it I started using the link to "Test web app for your latest code.". I forgot to add the parameters to the link. Very stupid. Thanks for your help, anyway. I guess you noticed my comment on your answer. – Punchlinern Feb 15 '14 at 18:13

1 Answers1

2

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);
}
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • The thing is, as I mentioned in passing in the beginning of my post, I'm trying to create a way to make an URL to a specific cell. So I want it to open the specified spreadsheet and set the selection from the GET-data. – Punchlinern Feb 15 '14 at 18:05
  • And what if you simply add a return at the end to actually return something... for example the value in the selected cell... have a look at contentServicein the documentation.. In the mean time I consider this as answered, good luck :-) – Serge insas Feb 15 '14 at 21:45
  • Thanks for your help! I've tried returning various things (ss for example) but it doesn't work. I read about content service but from what I gathered it's only used to return text. I read about UI service as well, but I didn't give me enough to make it work. I've started a [new question](http://stackoverflow.com/questions/21813699/open-google-spreadsheet-via-google-apps-script) where I address this problem more directly. – Punchlinern Feb 16 '14 at 16:15
  • You cannot return spreadsheet as something you can access just like in the SS UI, but you can return values, stringified objects like arrays... what is the final idea behind your question? I saw your other post but I'm afraid it's a bit unclear what you want to do... and the answer you got is not really interesting... – Serge insas Feb 16 '14 at 17:19
  • My goal is to be able to create a URL to a cell, like in Excel, to open the sheet and select the cell. I'm creating a project Spreadsheet, and got two sheets this far, one project sheet and one customer sheet. I want to be able to click on a customer in the project sheet to show it in the customer sheet. – Punchlinern Feb 16 '14 at 17:28
  • Do you mean you'd want your browser to open a window with the other sheet? – Serge insas Feb 16 '14 at 17:43
  • Well, best would be if the cell in the customer sheet would be set as active in the current window, but from what I've gathered that isn't possible (am I wrong?). Otherwise yes, that's what I'm trying to achieve. – Punchlinern Feb 16 '14 at 17:46
  • "active" is not the right word, I would say "selected" right ? – Serge insas Feb 16 '14 at 18:13
  • Yeah, perhaps, I use the word "active" since the function is setActiveRange. – Punchlinern Feb 16 '14 at 18:15