Search code examples
google-apps-scriptgoogle-sheetsgoogle-forms

Set Default value for column each time a new row is created


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");

   } 
 } 

};

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");
    
    
     } 
    
    };