Bagzli
Bagzli

Reputation: 6569

Google Forms Script Reset & Update Style

I am trying to write a script that will delete all responses, start a new sheet and then style the new sheet by making the first row red color. Here is what I have so far:

function Initialize2() {
  var form = FormApp.getActiveForm();
  var ss = SpreadsheetApp.openById(form.getDestinationId()); 
  form.deleteAllResponses();
  form.removeDestination();
  form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId());
  form = FormApp.getActiveForm();

  ss = SpreadsheetApp.openById(form.getDestinationId()); 

  var sheet = ss.getSheets()[0];
  var range = sheet.getRange("A1:Z1");
  range.setBackground("red");
  sheet.setFrozenColumns(1);
}

The problem that I am facing is that it will create a new sheet, but the color update happens on the previously active sheet. How do I make it so that it updates the new active sheet?

Upvotes: 2

Views: 242

Answers (1)

user3717023
user3717023

Reputation:

By the time you select a sheet for color changes, the new sheet with form responses may not exist yet, because Sheets may postpone operations to improve performance. To ensure the creation has happened, call

SpreadsheetApp.flush();

before

var sheet = ss.getSheets()[0];

Upvotes: 3

Related Questions