繁体   English   中英

如何让 Google Web 应用程序重定向到表格中的特定单元格

[英]How to get a Google Web App to redirect to a specific cell in Sheets

主要目标

  • 拥有一个二维码库存系统,扫描后,该系统会定向到带有查询字符串的 Google Web 应用 URL
  • 此 Web 应用程序在电子表格中搜索查询字符串参数并重定向到与参数匹配的单元格

请注意,我不能简单地使用 URL 链接到单元格。 当物品出售时,行可能会被删除。 如果二维码指向 A3 并且 A2 中的商品出售,那么 A3 中的商品将变为 A2。

我的问题是我无法让 web 应用程序重定向到电子表格。

这是我的工作,这是我用于 web 应用程序的代码

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.

编辑:

这有效:

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

由于 web 应用程序在子框架内运行,我们需要在尝试使用location.replace之前指定window.top (感谢大师指出这一点。)

原答案:

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

我试图看看HtmlService.setXFrameOptionsMode在这里是否有任何区别,但它没有。 刚刚收到错误:

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

您通过HtmlService提供的任何脚本都将位于框架内并且无法重定向。

您可以设置用户可以单击的链接,或在 web 应用程序中显示工作表值,并从 web 应用程序更新它们(您可以在自己的应用程序中模拟工作表)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM