简体   繁体   English

将表单提交到工作表时重定向

[英]Redirect on submit of form to sheet

A similar question on here doesn't deal with my scenario specifically and code from a tutorial doesn't work as advertised.此处的类似问题并没有专门针对我的场景,教程中的代码也没有像宣传的那样工作。 I'm using Google Apps Script to write results from an HTML form to a Google Sheet using this .我正在使用 Google Apps 脚本将结果从 HTML 表单写入使用的 Google 表格。
On submit it loads the Google App JSON result.提交时加载 Google App JSON 结果。 The article mentions adding an event listener on submit to have a thank you alert.文章提到在提交时添加事件侦听器以获得感谢提醒。 That didn't work and I'd rather redirect users to a thank you page.那没有用,我宁愿将用户重定向到感谢页面。 I tried a onClick="window.location.href='page to load';"我尝试了一个onClick="window.location.href='page to load';" but that didn't work either.但这也没有用。 So, I feel like it should be added to the Google App script copied below.所以,我觉得应该将它添加到下面复制的 Google App 脚本中。 I tried a few things but none worked.我尝试了几件事,但都没有用。 One of my attempts are still in there.我的一个尝试仍然在那里。 It's about syntax and Google Apps forums and docs shed no light on exactly this.它是关于语法的,Google Apps 论坛和文档对此没有任何说明。

Here's the code:这是代码:

const sheetName = 'Sheet1'
const scriptProp = PropertiesService.getScriptProperties()

function intialSetup () {
  const activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet()
  scriptProp.setProperty('key', activeSpreadsheet.getId())
}

function doPost (e) {
  const lock = LockService.getScriptLock()
  lock.tryLock(10000)

  try {
    const doc = SpreadsheetApp.openById(scriptProp.getProperty('key'))
    const sheet = doc.getSheetByName(sheetName)

    const headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]
    const nextRow = sheet.getLastRow() + 1

    const newRow = headers.map(function(header) {
      return header === 'Date' ? new Date() : e.parameter[header]
    })

    sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow])

    return ContentService
      .createTextOutput(JSON.stringify({ 'result': 'success', 'row': nextRow }))
      .setMimeType(ContentService.MimeType.JSON)
  }

  catch (e) {
    return ContentService
      .createTextOutput(JSON.stringify({ 'result': 'error', 'error': e }))
      .setMimeType(ContentService.MimeType.JSON)
  }

  finally {
    lock.releaseLock()
  }
}
//attempt at redirect 
  function s() {
    window.location.href = 'https://thewebsite.com'
  }

As of September 16, 2021, you can no longer redirect users to another URL inside HTMLService based web apps.自 2021 年 9 月 16 日起,您无法再将用户重定向到基于 HTMLService 的 web 应用程序中的另一个 URL。

The allow-top-navigation keyword, which allows the content to navigate its top-level browsing context, is restricted and not set as an attribute in the sandbox. allow-top-navigation 关键字(允许内容在其顶级浏览上下文中导航)受到限制,未设置为沙箱中的属性。 If you need to redirect your script, add a link or a button for the user to take action on instead.如果您需要重定向脚本,请添加一个链接或按钮供用户执行操作。

Read more here . 在这里阅读更多

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

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