Home > Blockchain >  Determine the environment a Google App Script is run from
Determine the environment a Google App Script is run from

Time:03-09

How to determine which environment the script is run from. a) the Spreadsheet environment by menu option or b) from the script editor. I want to include something in catch that displays the error. If I run from spreadsheet and use console.log I have to go to script editor to see error. If i run from script editor and use getUi().alert() the message is displayed in spreadsheet environment but don't see it.

function test() {
  try {
    // some code
  }
  catch(err) {
    if( environment is spreadsheet ) {  // run from spreadsheet via menu 
      SpreadsheetApp.getUi().alert(err);
    }
    else {  // run from script editor
      console.log(err);
    }
  }
}

CodePudding user response:

In your situation, I thought that when Google Apps Script API is used, your goal might be able to be achieved. When Google Apps Script API is run, the script is as follows.

Usage:

1. Linking Google Cloud Platform Project to Google Apps Script Project for New IDE.

In this case, it is required to link Google Cloud Platform Project with Google Apps Script project. This flow can be seen at my report.

2. Sample script.

// This function uses Google Apps Script API.
function check() {
  const scriptId = ScriptApp.getScriptId();
  const url = "https://script.googleapis.com/v1/processes?pageSize=1&userProcessFilter.scriptId="   scriptId;
  const res = UrlFetchApp.fetch(url, { headers: { authorization: "Bearer "   ScriptApp.getOAuthToken() } });
  const obj = JSON.parse(res.getContentText());
  return obj.processes[0].processType;
}

function main() {
  Utilities.sleep(2000); // When this wait is not used, the data of "processes.list" is not updated. So I added this. Please be careful this.
  const ui = SpreadsheetApp.getUi();
  const processType = check();
  if (processType == "EDITOR") {

    // When this script is run with the script editor, this if statement is true.
    ui.alert("Run with script editor.");

  } else if (processType == "MENU") {

    // When this script is run with the custom menu, this if statement is true.
    ui.alert("Run with custom menu.");

  } else {

    // When this script is run without the script editor and the custom menu, this if statement is true.
    ui.alert("Run without script editor and custom menu.");

  }
}
  • When this script is run with the script editor, ui.alert("Run with script editor.") is run.
  • When this script is run with the custom menu, ui.alert("Run with custom menu."); is run.

Note:

  • This script is a simple script. So please modify this for your actual situation.

  • When I tested this script, I confirmed that no issue occurred. But if you tested this script and an error occurs, please confirm my proposed script and the flow for linking GCP to GAS, again.

  • This sample script assumes that your script is the container-bound script of Google Spreadsheet. Please be careful about this.

Reference:

CodePudding user response:

A simple solution is to use nested try / catch

function test(){
  try {
     // do something

  } catch (error){

     try {
        SpreadsheetApp.getUi().alert(' Something ');
     } catch (uiError){
       throw error
     }
  }
}

The term used for "environment" by Google Apps Script is "context" i.e. when trying to execute SpreadsheetApp.getUi().alert(something) from a time-driven trigger or custom function, the script will throw the following error:

Cannot call SpreadsheetApp.getUi() from this context

Related

  • Related