0

I am using Goolge Apps Script to run a leads form that (1) gathers signup information, and (2) then shows the user the next page (which is a survey) after the complete the signup

My objective is to redirect the user to that survey page after they submit the form, however it is returning the following after they submit the form ["see image"].

I understand there are a few ways to redirect to another link, however I'm not sure which one to use or where to put that information within the body of this script (where does it go?)

`

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

function initialSetup () {
  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()
  }
}

`

I tried adding the following line of code to multiple lines in the script, and none of them seem to allow it to process.

window.location.replace("http://www.google.com");

I just want it to redirect instead of showing the result above. Thanks!

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Why don't you just change what the ContentService is returning now. – Cooper Oct 25 '22 at 20:11
  • I have tried deleting the `.createTextOutput` and replace it with the `window.location.replace("http://www.google.com");` and I'm not seeing any difference. What would I put there to redirect the user? – Relife Academy Oct 25 '22 at 20:42
  • I would guess that you would have to edit your deployment. Did you do that? – Cooper Oct 25 '22 at 21:15

1 Answers1

1

window.location.replace("http://www.google.com"); only works on client-side code, in other words, when using doGet instead of doPost and HtmlService instead of ContentService and calling the Google Apps Script web app from the address bar of a web browser.

Related

References

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • So, if I replace `doPost` with `doGet` and replace `ContentService` with `HtmlService`, where would I put the `window.location.replace("http://www.google.com");` ? – Relife Academy Oct 26 '22 at 01:04
  • It's not just a "find and replace". I added a couple of related questions that make use "window.location". If you need further help, I suggest you to post a new question including a [mcve] showing how you tried to use doGet, HtmlService and window.location. – Rubén Oct 26 '22 at 01:16