1

Is there a way I can access data, stored within a spreadsheet-file from the library script?

I want to use 1 Google Apps Script from multiple Google Spreadsheet files within my Google Drive.

I followed this answer: "you could use Libraries. The idea is that you create one script that you use as a library" and could successfully import the library to my project.

In order to work, the scripts within the library need some of the cell-values stored in the google sheet files. I know how to access the script via a helper function in my sheet-bound script file. For example:

function loc_my_credits()
{
  SISTRIXAbfrageFreigabe.my_credits();
}

Whilst "SISTRIXAbfrageFreigabe" is the library name, and my_credits a function within the library.

When I call loc_my_credits from sheets, nothing happens. My best guess: the script cant read data from the spreadsheet file it needs to execute.

The my_credits script from the library file looks like this:

function my_credits(){

// Base URL to access customsearch
  var urlTemplate = "https://api.sistrix.com/credits?api_key=%KEY%";

  // initialize sheets: 1. Get the spreadsheet, 2. Get the first and the second sheets in this spreadsheet
  var spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
  var inputSheet = spreadSheet.getSheets()[0];

  // Script-specific credentials & search engine
  var sistrix_Apikey = inputSheet.getRange('A2').getValue();

  var url = urlTemplate.replace("%KEY%", encodeURIComponent(sistrix_Apikey));
  
  var params = {
    muteHttpExceptions: true
  };

  var xml = UrlFetchApp.fetch(url).getContentText();  
  var document = XmlService.parse(xml);
  var root = document.getRootElement();  
  var items = document.getRootElement().getChildren();
  
  for (var i = 0; i < items.length; i++) {
    if(items[i].getName() == 'answer'){
      var answer = items[i].getChildren();
      return answer[0].getAttribute('value').getValue();
    }
  }
  
  return 0; 
}

Is there a way I can access the data stored in the spreadsheet file from the library script?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
s.Panse
  • 37
  • 9
  • This works for me: https://stackoverflow.com/a/63044171/10595326 But I cant access the spreadhsset - permission error. I tried to update the manifest, but I cant save the file. Manifest including permission settings for spreadheet: `{ "timeZone": "Europe/Berlin", "dependencies": {}, "exceptionLogging": "STACKDRIVER", "runtimeVersion": "V8", "oauthScopes": [ "https://www.googleapis.com/auth/spreadsheets.readonly", "https://www.googleapis.com/auth/spreadsheets" ] }` – s.Panse Jan 30 '23 at 21:11
  • 1
    I have to apologize for my poor English skill. I cannot understand the detail of `When I call loc_my_credits from sheets, nothing happens.`. In your situation, how did you run your script of `loc_my_credits()`? – Tanaike Jan 31 '23 at 00:33
  • Hi @Tanaike, tanks for your reply. The situation chenged in the meantime: at least I got an error message: Exception: You do not have permission to call SpreadsheetApp.openById. Required permissions: https://www.googleapis.com/auth/spreadsheets (Zeile 243). What I did: I call a function in from my sheet-bound script: `loc_my_credits()` The function looks like this: `function loc_my_credits() { var id = SpreadsheetApp.getActiveSpreadsheet().getId(); SISTRIXAbfrageFreigabe.my_credits(id); }` The function calls "my_credits(id)" in library "SISTRIXAbfrageFreigabe" – s.Panse Jan 31 '23 at 08:41
  • You mentioned this: `I tried to update the manifest, but I cant save the file`. Do you get any error when updating the manifest? – Fernando Lara Jan 31 '23 at 13:09
  • Is the error message the same you mentioned before about the missing scope? I can see the new manifest is different from the previous one as you are not including the requested scope from the error message. Are you adding the scope following [these steps](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes)? – Fernando Lara Jan 31 '23 at 15:12
  • Hi @FernandoLara, thanks again! You are right, meanwhile I playeed arround with the manifest and the version I use now differs. The error message reds: `Exception: You do not have permission to call SpreadsheetApp.openById.` I also tried the shorter oAuth scope from Googles help page: `"https://www.googleapis.com/auth/spreadsheets.readonly", "https://www.googleapis.com/auth/userinfo.email"` Same error. – s.Panse Jan 31 '23 at 16:00
  • Ah, Frack! Sorry @FernandoLara. I pasted the wrong manifest file before. The manifest file of the library: `{ "timeZone": "Europe/Berlin", "dependencies": {}, "exceptionLogging": "STACKDRIVER", "runtimeVersion": "V8", "oauthScopes": [ "https://www.googleapis.com/auth/script.container.ui", "https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/script.external_request" ] }` – s.Panse Jan 31 '23 at 16:05
  • How are you executing the function? Are you using simple triggers? – Fernando Lara Jan 31 '23 at 16:45
  • This funtion, giving me the error message is triggered from spreadsheet. I call it from a cell. Not directly, but via a helper function in the bound script. The helper funtion calls the function which sits in a library. Does that make sense? @FernandoLara – s.Panse Jan 31 '23 at 18:34
  • I had to change the way the "helper functions" in the sheet-bound script call the functions within the library. This is done via installable triggers. Those call the helper-function and triffer the function in the library (for example) whenerver the sheet is being modified. [Answer](https://stackoverflow.com/a/27760863/10595326) @FernandoLara – s.Panse Jan 31 '23 at 20:46
  • 1
    Thank you for replying. Now, I noticed that the discussion has already been advanced. In this case, I would like to respect the existing discussion. – Tanaike Feb 01 '23 at 00:18
  • @Tanaike, thank you guys for your help! I know, my ticket and comments are confusing, sorry for that. Should I write a short answer and link to the correct comment, or is this not necessary? – s.Panse Feb 01 '23 at 07:01
  • Thank you for replying. If you have an answer, How about posting it? It will be useful for other users who have the same issue. – Tanaike Feb 01 '23 at 07:45
  • @s.Panse I just posted what I think it was your solution from the comments as a community wiki just for future references in case someone else has the same issue. – Fernando Lara Feb 01 '23 at 15:47

1 Answers1

1

I'm writing this answer as a community wiki, since the issue was resolved from the comments section, in order to provide a proper response to the question.

The problem was related to the usage of methods that need scopes that require authorization, therefore it would be expected that simple triggers would show the error:

You do not have permission to call SpreadsheetApp.openById

Google's documentation states that installable triggers would solve the problem:

Installable triggers, however, offer more flexibility than simple triggers: they can call services that require authorization

According to s.Panse, the usage of installable triggers has resolved the issue in this case.

References:

Fernando Lara
  • 2,263
  • 2
  • 4
  • 14