onSelectionChange and UrlFetchApp

508 views
Skip to first unread message

Kevin -

unread,
Aug 9, 2022, 6:56:22 AM8/9/22
to Google Apps Script Community
Please bear with me. I know this is wordy... I've read every post and doc I can find that addresses the error: "Exception: You do not have "permission to call UrlFetchApp.fetch. Required permissions: https://www.googleapis.com/auth/script.external_request". The project manifest (for the Google Sheet app) contains the appropriate oauthScopes entry for the required permissions noted. The permissions have been granted via the Google prompt. I realize (from the Gdocs and many posts) that simple triggers cannot call urlFetchApp successfully, but the urlFetchApp instruction is not in the onSelectionChange trigger event function; it's in a function called by the trigger. I guess that's the same thing... Listed below is the onSelectionChange function code. It's the call to  "exportRangeToPDf(range)" that contains the urlFetchApp instruction that produces the error. BTW, the exportRangeToPDF works fine when I set the active (selected) range and run it from the editor. Creating an installable trigger does not work because there is no onSelectionChange equivalent -- onChange does not trigger when a cell is simply clicked on, but only when " a user modifies the structure of a spreadsheet itself—for example, by adding a new sheet or removing a column". I just want to print the range the user selected (a calendar week starting on Sun). Is there an installable trigger that fires when a cell is clicked on? Some other way?

function onSelectionChange(e) {    
  var r = e.range;
  var v = r.getValue();
  var ss = e.source.getActiveSheet();
  var r1c1, res, range=[];
  if (v == 'Sun') {
    res = alertMsg("🛑 Action Required","You clicked a Sunday - "+r.getA1Notation()+". Click YES to Print the Week; NO to Count the Week","YES_NO_CANCEL");
    if (res == "NO") { // Count the range
      r1c1 = a1ToR1C1(e.range.getA1Notation());    
      range = ss.getRange(3,r1c1.column,15,12); // The week starting with Sunday clicked
      countSelectedRange(range);
    } else if ( res == "YES" ) { // Print the range
      var r1c1 = a1ToR1C1(e.range.getA1Notation());      
      var range = ss.getRange(1,r1c1.column,19,12);
      exportRangeToPDf(range);
    }
  }
}

CBMServices Web

unread,
Aug 9, 2022, 2:28:55 PM8/9/22
to google-apps-sc...@googlegroups.com
Hi Kevin,

I am afraid there is no other option to you on this. Simple Triggers can not call any outside services besides poking at the spreadsheet itself.

I am just wondering why you are doing it this way rather than maybe just putting in a button to do the printing. You can call the function from a button and not be limited by the simple trigger restrictions.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/b4c0f0f7-1b95-4ffa-b766-3d7f2923d64en%40googlegroups.com.

Kevin -

unread,
Aug 10, 2022, 2:46:34 PM8/10/22
to google-apps-sc...@googlegroups.com
"Simple Triggers can not call any outside services besides poking at the spreadsheet itself." -- Why is that? Is it some sort of security restriction? Seems unnecessary, but what do I know? The reason I did it this way was convenience for the user and simplicity for me: The user first selects which calendar week to print; this is done by simply clicking on the Sunday cell that starts with week; then the print function is called. It looks like I will have to reverse the order: User selects Print Week from the menu and the printer function includes a call to HTML UI that lists the current month's weeks, returning the selected week to the print function which uses that for the range to print. I was hoping to avoid all that HTML, CSS, client-side JS. Oh well. Thanks for the reply. Appreciate it.

You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/u79DV-Ko4rw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CAFX%2Bz3UbL9WSgqnpN0DCTRyGKpvUDZ%3DENamN9rDNtYM2ssm%2BcA%40mail.gmail.com.

cbmserv...@gmail.com

unread,
Aug 10, 2022, 2:51:33 PM8/10/22
to google-apps-sc...@googlegroups.com

You don’t need any html/css if you just want to put a button.

 

Just go to insert/drawing

 

Add a shape that you like and put a label on it and add it into your spreadsheet, then click on 3 dots besides it and select the script function you want it to call when it is pressed.

 

That will call that function for you and do whatever action you want with this manual trigger..

Kevin -

unread,
Aug 13, 2022, 9:58:10 AM8/13/22
to Google Apps Script Community
Adding a button for each "Sunday" cell that needs the trigger function for every month sheet would be possible, I suppose, but unless it can be done programmatically, it's not the solution. It's easier just to select the week range and click the Print button, etc to create the PDF to print. That drawing/image/assign script solution is really unnecessary with onSelectionChange.
Reply all
Reply to author
Forward
0 new messages