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

Multilple forms going into different sheets on the same document


My problem is slighty simple but i ve been stuck with this small issue, i ve got multiple forms connected to a sheet. Forms Responses when submited get reorganized and depending of answers something different will appear in the sheet, the thing is i ve been using this script to make the difference between each and i was wondering if it would be posible for instead of using the ID of the sheet, if i could use the Name of the sheet, since i ve been planning of making a copy of the spreadsheet, and would like it to be easy to give to someone, with the new copy of the form with out needing to recode anything.

function onFormSumbit(e) {
  var sh = SpreadsheetApp.getActiveSheet();
  if (sh.getSheetId() == 2) {
    function A();
  }
  else if (sh.getSheetId() == 4) {
    function B();
  }
}

Solution

  • It is possible to use sheet name in your if statement instead of sheet id. You can use getSheetName(). Your code should look like this:

    function onFormSumbit(e) {
      var sh = SpreadsheetApp.getActiveSheet();
      if (sh.getSheetName() == 'Sheet1') {
        function A();
      }
      else if (sh.getSheetName() == 'Sheet2') {
        function B();
      }
    }