1

MAIN GOAL:

  • Have an inventory system of QR Codes that, when scanned, directs to a Google Web App URL with a query string
  • This Web App searches a spreadsheet for the query string parameters and redirects to the cell that matches the parameter

Note that I can not simply use a URL linking to a cell. Rows are subject to be deleted as items sell. If a QR code directs to A3 and the item in A2 sells, then the item in A3 would become A2.

My problem is that I can't get the web app to redirect to the spreadsheet.

Here is my sheet & this is the code I'm using for the web app

function getCell(item, url){
  
  var sheet = SpreadsheetApp.openByUrl(url).getSheetByName("Sheet1");
  var lastRow = sheet.getLastRow();
  var range = sheet.getRange('A1:A' + lastRow).getValues();
  
  for(var i = 0; i < lastRow; i++){
    if(range[i] == item){
      return 'B' + (i + 1);
    }
  }
}

function doGet(e){
  var sheetUrl = "https://docs.google.com/spreadsheets/d/1K6xva3BRzxTAchXi1iseU0MiYCM4luN4edXCgmEv8MU/edit#gid=0";
  var cell = getCell(e.parameter.item, sheetUrl);

  return HtmlService.createHtmlOutput('<script>window.location.replace("' + sheetUrl + '&range=' + cell + '"</script>');
}

I'm trying this URL https://script.google.com/macros/s/AKfycbwdIZ8xbtf9iBHvNehcbJiYUE1E1gvDR7dyjPRM2CCGH1q_IwQ/exec?item=Dumbell_4 And it doesn't take me past the web app page.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Ryan
  • 15
  • 4
  • 1
    I think you will have to have the Spreadsheet Open up by the user using the WebApp and then have a function that takes you to something like this: `SpreadsheetApp.openByUrl(url).getRange('SheetName'+ '!' + Range.getA1Notation()).activate();` When a webapp opens a spreadsheet the user interface doesn't open up. It only opens on the server ie no user. – Cooper Jul 18 '20 at 02:44
  • In that case you might as well be using a dialog. – Cooper Jul 18 '20 at 02:50
  • The process would take place on phones and I don't think a Sheets app is affected by Safari. Would you know a way to simply display a link on the web app page as per @dwmorrin's answer? Something like `https://docs.google.com/spreadsheets/d/1K6xva3BRzxTAchXi1iseU0MiYCM4luN4edXCgmEv8MU/edit#gid=0&range=A4` – Ryan Jul 18 '20 at 03:39
  • Does this answer your question? [Can't stop Google Apps Script from masking redirected URL](https://stackoverflow.com/questions/56685553/cant-stop-google-apps-script-from-masking-redirected-url) – TheMaster Jul 18 '20 at 04:24
  • Where do I put `window.top.location = url` at? – Ryan Jul 18 '20 at 05:16
  • `script>window.top.location.replace...` – TheMaster Jul 18 '20 at 14:17

1 Answers1

3

Edit:

This works:

function doGet() {
  return HtmlService.createHtmlOutput('<script>window.top.location.replace("https://www.google.com")</script>');
}

Since the web app runs inside a subframe, we need to specify window.top before trying to use location.replace. (Thank you TheMaster for pointing this out.)

Original answer:

Apps Script web apps have restrictions that will prevent you from using window.location.replace to go to the URL of the Sheet.

I tried to see if HtmlService.setXFrameOptionsMode made any difference here, and it did not. Just received the error:

Refused to display 'https://www.google.com/' in a frame because it set 'X-Frame-Options' to 'sameorigin'.

Any script you serve via HtmlService is going to be inside a frame and unable to redirect.

You can setup links that the user can click on, or display the sheet values in your web app and update them from the web app (you can emulate the Sheet in your own we app).

dwmorrin
  • 2,704
  • 8
  • 17
  • Thank you! Could you help me set up a link? How would I display e.g. `https://docs.google.com/spreadsheets/d/1K6xva3BRzxTAchXi1iseU0MiYCM4luN4edXCgmEv8MU/edit#gid=0&range=A4`? – Ryan Jul 18 '20 at 03:27
  • @Ryan - Literally "how to make a link"? Example html link: `I am a link to Ryan's spreadsheet`. Sorry if you already know that and I'm misunderstanding what you're asking. If you're asking for a Web App tutorial, [work through the Apps Scripts guides, especially on using the HTML templates](https://developers.google.com/apps-script/guides/html/templates). If you get stuck, ask a *new* question and review [how to ask](https://stackoverflow.com/help/how-to-ask). – dwmorrin Jul 18 '20 at 11:19
  • You should use `window.top.replace` to replace the top frame. – TheMaster Jul 24 '20 at 19:09
  • @TheMaster - link to more info on using `window.top.replace`? Whether in the web app or just in the dev console, `window.top.replace === undefined` for me... edit: nevermind, I got it with `window.top.location.replace`. – dwmorrin Jul 24 '20 at 19:17