【问题标题】:How to get a Google Web App to redirect to a specific cell in Sheets如何让 Google Web App 重定向到表格中的特定单元格
【发布时间】:2020-11-07 20:22:02
【问题描述】:

主要目标

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

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

我的问题是我无法让网络应用重定向到电子表格。

这是我的sheet,这是我用于网络应用的代码

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

我正在尝试这个 URL https://script.google.com/macros/s/AKfycbwdIZ8xbtf9iBHvNehcbJiYUE1E1gvDR7dyjPRM2CCGH1q_IwQ/exec?item=Dumbell_4 而且它不会让我通过网络应用程序页面。

【问题讨论】:

  • 我认为您必须让用户使用 WebApp 打开电子表格,然后有一个功能将您带到这样的地方:SpreadsheetApp.openByUrl(url).getRange('SheetName'+ '!' + Range.getA1Notation()).activate(); 当 web 应用程序打开电子表格时,用户界面没有打开。它只在服务器上打开,即没有用户。
  • 在这种情况下,您还不如使用对话框。
  • 该过程将在手机上进行,我认为表格应用不会受到 Safari 的影响。您是否知道根据@dwmorrin 的回答在 Web 应用程序页面上简单地显示链接的方法?类似https://docs.google.com/spreadsheets/d/1K6xva3BRzxTAchXi1iseU0MiYCM4luN4edXCgmEv8MU/edit#gid=0&amp;range=A4
  • 我应该把window.top.location = url 放在哪里?

标签: google-apps-script google-sheets


【解决方案1】:

编辑:

这行得通:

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

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

原答案:

Apps Script web apps have restrictions 将阻止您使用 window.location.replace 转到工作表的 URL。

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

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

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

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

【讨论】:

  • 谢谢!你能帮我建立一个链接吗?我将如何显示例如https://docs.google.com/spreadsheets/d/1K6xva3BRzxTAchXi1iseU0MiYCM4luN4edXCgmEv8MU/edit#gid=0&amp;range=A4?
  • @Ryan - 字面意思是“如何建立链接”?示例 html 链接:&lt;a href="https://docs.google.com/spreadsheets/d/1K6xva3BRzxTAchXi1iseU0MiYCM4luN4edXCgmEv8MU/edit#gid=0&amp;range=A4"&gt;I am a link to Ryan's spreadsheet&lt;/a&gt;。抱歉,如果您已经知道,我误解了您的要求。如果您需要 Web 应用教程,work through the Apps Scripts guides, especially on using the HTML templates。如果遇到困难,请提出问题并查看how to ask
  • 你应该使用window.top.replace替换顶框。
  • @TheMaster - 链接到使用window.top.replace的更多信息?无论是在 Web 应用程序中还是仅在开发控制台中,window.top.replace === undefined 对我来说...编辑:没关系,我用 window.top.location.replace 得到它。
猜你喜欢
  • 1970-01-01
  • 2015-10-15
  • 1970-01-01
  • 2013-09-21
  • 2012-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多