Stoan
Stoan

Reputation: 1

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.
On submit it loads the Google App JSON result. 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';" but that didn't work either. So, I feel like it should be added to the Google App script copied below. 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.

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

Upvotes: 0

Views: 1049

Answers (1)

Amit Agarwal
Amit Agarwal

Reputation: 11268

As of September 16, 2021, you can no longer redirect users to another URL inside HTMLService based web apps.

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. If you need to redirect your script, add a link or a button for the user to take action on instead.

Read more here.

Upvotes: 1

Related Questions