-2

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);
    }
  }
}

I tried following the GCP steps outlined in Tanaike's report but can not get it to work. I have attached a Google doc containing screen captures of my GCP attempt. I can not seem cut and paste images to this post.

GCP screen captures

TheWizEd
  • 7,517
  • 2
  • 11
  • 19

2 Answers2

2

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:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Its not your poor skills its mine. I am not that familiar with the GCP process. I have struggled with it to try to publish add-ons I have developed. I am not a professional programmer just a hobbiest so my skills are limited. – TheWizEd Mar 09 '22 at 13:19
  • @TheWizEd Thank you for replying. I deeply apologize for the inconvenience. I think that your skill is very high. For example, in your situation, you are using your personal account? If you are using the account in the organization, I'm worried that you have no permission for liking GCP with GAS project. How about this? – Tanaike Mar 09 '22 at 13:38
  • Is GCP a subscription service or can I create a GCP from my personal account. The process is convoluted and I seem to have to go from page to page to try to create project, OAuth and maybe more beyond that. I just have to keep trying. But thank you for all your assistance and patience with me. – TheWizEd Mar 09 '22 at 14:20
  • @TheWizEd Thank you for replying. In [my report](https://github.com/tanaikech/Linking-Google-Cloud-Platform-Project-to-Google-Apps-Script-Project-for-New-IDE), the personal account (gmail account) is used. In this case, GCP can be linked with GAS project. – Tanaike Mar 09 '22 at 14:25
0

Apparently you want to see all the Error object properties in an Google Sheets alert. To get this, instead of

SpreadsheetApp.getUi().alert(err);

use

SpreadsheetApp.getUi().alert(JSON.stringify(err));

The above because the alert parameter should be string, but try / catch is getting Error object, so, by the JavaScript rules, the object is converted into an string by using the Error prototype method, so it's printing err.message.

If you also want to log the error in the Google Apps Script execution logs add console.error(JSON.stringify(err) before the above statement.

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166