Home > Net >  How to Rename Form Response Destination Sheet with Google App Script
How to Rename Form Response Destination Sheet with Google App Script

Time:01-29

I have script to generate Google Form and set where the Destination Form Response. After set, I want to rename the Sheet Name, from default Response Sheet Name ( eg. Form Response 1), with custom Sheet Name.

function genarateForm() {
var ss          = SpreadsheetApp.getActive();             
var sheet       = ss.getSheetByName('CONSUMEN DATA'); 
var rowNumber   = (sheet.getDataRange().getNumRows())-3; 
var myQuestions = sheet.getRange(5,2,rowNumber,1).getValues(); 
var form        = FormApp.create('CUSTOMER BILL'); 
const formTitle = 'BILL REPORT'
form.setTitle(formTitle)
  .setDescription('Fill with customer bill :')
  .setAllowResponseEdits(true)
  .setAcceptingResponses(true);

for(var i=0;i<rowNumber;i  ){
var addItem = form.addTextItem();

addItem.setTitle(pertanyaanKu[i]);
     }

form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId())
   
  // I want to set the sheet destination response with "CUSTOMER BILL"
}

CodePudding user response:

In order to achieve your goal, how about the following modification?

From:

form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId())

To:

form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId());

// I added the below script.
SpreadsheetApp.flush(); // This might not be required to be used.
const formUrl = form.getEditUrl().replace("edit", "viewform");
const formSheet = ss.getSheets().find(s => s.getFormUrl() == formUrl);
if (formSheet) {
  formSheet.setName("CUSTOMER BILL");
}
  • When this script is run, the sheet name of a sheet of the created Google Form is changed to "CUSTOMER BILL".
  • Related