Home > Software design >  Rename spreadsheets created in AppScripts Forms
Rename spreadsheets created in AppScripts Forms

Time:10-13

I am using a script that generates programatically some forms. I am registering the answers in a separate google spreadsheet. The problem is, whenever a new form is created, a new spreadsheet is created in my spreadsheet too. So, one spreadsheet for each form created. Is there any way to rename the new sheets created so that I'll be able to know which sheet corresponds to the created form.

Or even better, log all the responses in just one master sheet.

My appscript file is a standalone file:

I log the submitted responses with:

destination=SpreadsheetApp.openById('id');
form.setDestination(FormApp.DestinationType.SPREADSHEET, destination.getId())

CodePudding user response:

The problem is, whenever a new form is created, a new spreadsheet is created in my spreadsheet too. So, one spreadsheet for each form created.

I don't see that happening in the code provided. I can only see additional response sheets due to forms being linked in the same spreadsheet.

If what you mean is rename the created sheet on the same spreadsheet where the form is linked in your code, then see below approach.

Code:

function myFunction() {
  revisedSheetName = 'revised name';
  id = 'id'
  form = FormApp.create('test form');
  destination = SpreadsheetApp.openById(id);

  // get old set of sheets before form linking
  oldSheets = destination.getSheets().map(sheet => sheet.getSheetName());
  form.setDestination(FormApp.DestinationType.SPREADSHEET, id)
  // apply pending sheet creation
  SpreadsheetApp.flush()
  // get new set of new sheets (including new creation)
  newSheets = destination.getSheets().map(sheet => sheet.getSheetName());

  // get name of new sheet by getting the difference of new and old sets of sheets
  newSheetName = newSheets.filter(sheet => !oldSheets.includes(sheet));
  // access new sheet, then rename
  destination.getSheetByName(newSheetName).setName(revisedSheetName);
}

Sample:

sample

Output:

output

  • Related