0
votes

The script below is container-bound to a Google Sheets spreadsheet. When the user clicks a button on the sheet, a modal dialog is launched to gather input. This input is used to make a copy of another spreadsheet. At the end of the script an alert appears to tell the user where to find the new spreadsheet that has been created. Currently, all these spreadsheet reside in a Google Shared Drive. Eventually they'll need to reside in a regular Google Drive from which they can be shared, but I'm trying to get this issue sorted first before I debug in that setting.

function scheduleAccount() {
  // Request user input to generate name of new spreadsheet
  var htmlOutput = HtmlService.createHtmlOutputFromFile('scheduleAccountUI')
    .setWidth(300)
    .setHeight(300);
  SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Account Information');

}

function checkIfAccountScheduleExists(values) { //values is an object containing name-value pairs returned by the form in scheduleAccountUI.html
  console.log(values);

  var current_file = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId());
  var current_folder = current_file.getParents().next();
  console.log('Current folder: ' + current_folder);

  // Check whether a file already exists with the same name
  var filename = values["acctref"] + ' Schedule';
  var schedule_exists = current_folder.getFilesByName(filename).hasNext();
  console.log('Schedule exists?: ' + schedule_exists);
  if (schedule_exists) {
      SpreadsheetApp.getUi().alert("Oops - it looks like you've already created a schedule for that account.");
  } else {
   createNewAccountSchedule(values, filename, current_folder);
  }

}

function createNewAccountSchedule(values, filename, current_folder) { //values is an object containing name-value pairs returned by the form in scheduleAccountUI.html

  // Make a copy of the Account Schedule template
  var template = DriveApp.getFilesByName('Template - Account Schedule').next();
  var new_schedule_ID = template.makeCopy(filename, current_folder).getId();
  console.log('New schedule ID: ' + new_schedule_ID);

  // Tell user where to find the new spreadsheet  
  SpreadsheetApp.getUi().alert("Template created! You'll find it in the same folder as this dashboard, with the filename " + values["acctref"] + " Schedule.");
  console.log('Alert displayed');

}

Everything works fine when I run the script myself. However, when another user who is an editor but not owner of the spreadsheet runs it, the modal dialog appears, and everything else in the script runs as intended, but the final alert never appears. According to Stackdriver logs, the script times out waiting for user response.

screenshot of Stackdriver logging

If I run the script in a context that should trigger the "Oops, that schedule already exists" alert, that alert doesn't appear, either.

I couldn't find anything in the documentation about special permissions being needed for alerts. Does anyone know what could be causing this behavior? How can I get the alert to display for all users?

1

1 Answers

0
votes

An answer to this question turned out to be the answer to mine as well, although my script wasn't throwing an error. I logged out of all Google accounts and logged back in to only the editor/non-owner account, and the alert displayed as intended. I'm leaving this question and answer here in case it helps someone else.