Question

I have a Google form set up which puts the responses into a Google Spreadsheet, I have added a few extra columns to the sheet to help us monitor the status of each response. Is there a way I can set the default value for a column each time a new row is created? (i.e. each time a new response is submitted). Below is the code I have so far, but currently it only does it for one row.

function setDefault() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var lastRow = ss.getLastRow();

 for (var i = 3; i <= lastRow; i++)
 {
   sheet.getRange('A' + i).activate();
   var value = sheet.getActiveCell().getValue();

   while (value != "")
   {
    sheet.getActiveCell().offset(0, 5).activate();
    sheet.getActiveCell().setValue("Unknown");
    sheet.getActiveCell().offset(0, 1).activate();
    sheet.getActiveCell().setValue("New");

   } 
 } 

};
Was it helpful?

Solution

I solved it using the following code and set the project trigger to "On form submitted" so that it runs each time a new response is submitted.

function setDefault() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var lastRow = ss.getLastRow();

 for (var i = 3; i <= lastRow; i++)
 {
   sheet.getRange('A' + i).activate();

    sheet.getActiveCell().offset(0, 5).activate();
    sheet.getActiveCell().setValue("Unknown");
    sheet.getActiveCell().offset(0, 1).activate();
    sheet.getActiveCell().setValue("New");


 } 

};

OTHER TIPS

The method through last row might have troubles that the form submission isn't always the last row if you manually edit the spread sheet or if a user edit his response! That happens a the time ><"

I suggest another approach, try this:

function onFormSubmit(e){
      var r = e.range.rowStart;
      Logger.log(r);
  }
}

Actually you can just log out the variable "e" to find out more submission informations @@b

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top