Google sheet is not receiving form submits

ε祈祈猫儿з 提交于 2020-01-06 05:31:14

问题


I created the sheet: https://docs.google.com/spreadsheets/d/1aghZREjkKSNkpCfWJUw_AMs1ZEoTIZOhDca4UHvuVtU/edit?usp=sharing

and from the script editor I created the below form:

Code.gs

function doGet() {
    return HtmlService.createTemplateFromFile('Form.html')
        .evaluate() // evaluate MUST come before setting the Sandbox mode
        .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}


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


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

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

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

    var newRow = headers.map(function(header) {
      return header === 'Timestamp' ? 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()
  }
}

Form.html

<!DOCTYPE html>
<html>
<body>
<style>
</style>

<form name="submit-to-google-sheet" id="form" method="POST" onsubmit="myFunction()">
<input name="TEXT" type="text" placeholder="text" required>
  <input type="submit" value="Submit" name="submit" id="Submit">
</form>

<script>
function myFunction() {
  alert("The form was submitted. Please press okay to reload the page");
}
</script>

<script>
  const scriptURL = 'https://script.google.com/macros/s/AKfycbxdtFz3L5Zczor9v-CGvm1yLzTogasSF__22oadV80ZFMQFH18/exec'
  const form = document.forms['submit-to-google-sheet']
  form.addEventListener('submit', e => {
    e.preventDefault()
    window.open("URL after form submit", "_top")
    fetch(scriptURL, { method: 'POST', body: new FormData(form)})
      .then(response => console.log('Success!', response))
      .catch(error => console.error('Error!', error.message))
  })
</script>

</body>
</html>

The same code is working for me in another sheet but when I tried to repeat in this sheet for some reason the form is not submitting to the sheet. Any help? And please would you give instructions in the proper way to copy the same code to other sheets so when I create another sheet with different columns and pasting the same code to the script editor, what needs to be done to make the script works for the new sheet and so on. Thanks


回答1:


There are thwo things you need to change:

  1. When you copy your script to another spreadsheet and publish it as a WebApp - scriptURL is going to change - please update it with the new one.

  2. var doc = SpreadsheetApp.openById(scriptProp.getProperty('key')) is only going to work if you set up the property key for the new script. AS as workaround, just replace this line by var doc = SpreadsheetApp.getActive(); - this will make the script automatically find the spreadsheet it is bound to.




回答2:


Okay the solution is;

Keep the original script in the question as is with the below modifications:

Remove this part from the html:

<script> const scriptURL = 'Script URL' const form = document.forms['submit-to-google-sheet'] form.addEventListener('submit', e => { e.preventDefault() window.open("URL 
after form submit", "_top") fetch ... </script>

and add the script URL here:

<form name="submit-to-google-sheet" id="form" action="Script URL" method="POST" 
onsubmit="myFunction()">

and for redirection purpose; replace this:

<script>
function myFunction() {
alert("The form was submitted. Please press okay to reload the page");
}
</script>

with

<script> function myFunction() { alert("The form was submitted. Please press okay to reload the page"); window.open("https://www.example.com", "_top"); } </script>

thanks to all friends for help ..



来源:https://stackoverflow.com/questions/59272746/google-sheet-is-not-receiving-form-submits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!