user2639740
user2639740

Reputation: 1215

Apps Script - Update the user on code execution status

Some of the scripts that I run take a long time and users might get concerned that a script stopped working if they can't see the status/step. I have added a spinner to the Sidebar to at least indicate that the script started running, but I would like to do more than that.

Ideally, I would be able to directly update the Sidebar contents from the GAS, but I gather than is not possible because of sandboxing. I have seen other questions and answers that discuss using success handlers in a daisy chain like this:

function uploadActivities(){
  google.script.run.withSuccessHandler(onSuccess).activities_upload();
}
function onSuccess(lastStatus){
  $('#codestatus').text(lastStatus);
  google.script.run.step_two();
}

It is a hack and it would require me to split the code into smaller steps and pass values to the UI, which don't belong in the UI, and back to the code. I really don't like that approach and maintenance could be a bear.

I have tried creating a var in GAS and updating that value as the code progresses. However, I can't find a way to get the UI to periodically check until the code execution is complete AND to successfully update the UI after each step.

Here is the code I have created:

function uploadActivities(){
  google.script.run.activities_upload();
  getStatus();
}
function getStatus(){
  var isActive = true;
  while(isActive){
    var lastStatus = google.script.run.getStatus();
    $('#codestatus').text(lastStatus);
    if(lastStatus === 'Complete'){ isActive = false; }
  }
}

In GAS I use this code:

var codeStatus = 'start';

function getStatus(){
  return codeStatus;    
}

function activities_upload(){
  codeStatus = 'Started Execution';
  ...
  codeStatus = 'Extracting Values';
  ...
  codeStatus = 'Uploading Activities';
  ...
  codeStatus = 'Complete';
}

It runs the required code, and even updates the #codestatus div with the first value, but it doesn't get any values beyond the first value. Additionally, it creates a continuous loop if there is an error in the code execution, so that isn't good either.

Is there a good, efficient, and safe way to complete this approach? Or, is there a better way to notify the user of the code execution status so they don't get worried if it takes a while, and can tell if there has been an issue?

Upvotes: 2

Views: 526

Answers (1)

davids
davids

Reputation: 5577

I have struggled with this for some time. Unfortunately, I don't have a good fix for your approach, but I can show what I finally did and it seems to be working.

First, create an easy way to send a toast to your users.

function updateStatus_(alert,title){
  var ui = SpreadsheetApp.getActiveSpreadsheet();
  var title_ = title!=""?title:"";
  ui.toast(alert,title_);
} 

Second, as required, use the toast to update the user.

function activities_upload(){
  updateStatus_('Started Execution');
  ...
  updateStatus_('Extracting Values');
  ...
  updateStatus_('Uploading Activities');
  ...
  updateStatus_('Complete');
}

This will alert the user with a temporary message as the code progresses and not require the user to clear an alert.

Please note that if the steps progress rapidly the user will see the toast flash on the screen only to be quickly replaced by the next toast. So, make sure you don't have too many throughout your execution.

Upvotes: 3

Related Questions