Google Forms App Script : How to check if current submission is editing response or a new response

前端 未结 2 1868
情书的邮戳
情书的邮戳 2021-01-13 18:23

I am working with google forms via app script. I want to determine if the current form that is being submitted is in edit mode or a new response? How can I check this in the

相关标签:
2条回答
  • 2021-01-13 18:57

    The Google Form form submit event doesn't have a field that could help to know if the response is a new response or if it's a response edit. As Sandy already said through question comments, the Form Servicer classes and methods, nor the response values include something that could help on this.

    By the other hand, the Google Sheets submit event has a range field that could help. The following script bounded to a Google spreadsheet logs the response row:

    function onFormSubmit(e){
      var response = e.range;
      Logger.log(response.getRow());
    }
    

    The above could be used to keep updated a column to hold a revision counter. If the corresponding cell is blank, then the response is a new response, other way it's a response edit.

    The following script it to be bounded to the spreadsheet that receives the Form responses. It requires a on form submit installable trigger. Other instructions to adapt it are included on the script comments.

    /*
     *
     * Global Variables
     *
     */
    
    /*
     * Sheet name used as destination of the form responses
     */
    var sheetName = 'Form Responses';
    
    /*
     * Name of the column to be used to hold the response revision counter 
     * It should match exactly the header of the related column, 
     * otherwise it will do nothing.
     */
    var revisionsColumn = 'Rev';
    
    /*
     * Responses starting row
     */
    var startRow = 2;
    
    function setRevisionCounts(e){
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
      var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues(); 
      var revisionsIndex = headers[0].indexOf(revisionsColumn);
      var data = sheet.getDataRange().getValues();
      var response = e.range;
      var rowIndex = response.getRow()-1;
      var rev = data[rowIndex][revisionsIndex]+1;
      sheet.getRange(rowIndex+1, revisionsIndex+1).setValue(rev); 
    }
    
    0 讨论(0)
  • 2021-01-13 19:14

    This is not really an answer but rather a 'heads up'. I have found that if you resubmit (edit) a form that data that is not changed is resent unchanged.

    Why does this matter? Let's say you have a branch in your form eg Do you like apples or bananas? If you like apples then the next question might be what is the colour of your car? Let's say 'red' of if you like bananas the next question might be how old are you? Let's say 35.

    So you choose apples and red and submit the form. Then you edit your reply and instead choose bananas and 35.

    When the form is resubmitted the new data still says car=red and (as expected) age=35.

    I would expect/hope that now car=null and age=35 but this is not the case. Car=red still appears against the updated row in the destination sheet.

    I hope this is useful to you and maybe someone will suggest a work around.

    0 讨论(0)
提交回复
热议问题