Deny granting permissions in my own google scripts

2019-03-03 17:59发布

问题:

Situation:

I have made my self a speadsheet to enter my working times. For some cases I have to enter some links and name them with part of the linkname. So I decided to create a custom menu where I simply post the link in a prompt and the script cuts out needed name and enters this to my sheet.

Now this is only running in my sheet. Guess there is no need to publish something like this :)

To my problem:

I have a main workingsheet and copy this every week because one sheet only solves one week. My script causes me to grant permissions to it so it can do upper described actions on current sheet. But everytime I copy the sheet (so every week) I have to grand the permissions again. Looking to my google account seeing granted permissions giving me headache since there are planty of entries for granting permissions :(

Question:

Is there a way to stay in kind of a developermode to prevent this permission requests?

Why do I have to grand permissions to my own script?

function onOpen(e) {
  var menu = SpreadsheetApp.getUi().createMenu('Custom Menu');
      menu.addItem('Add Ticket', 'addTicket');
      menu.addItem('Rename to KW XX', 'renameDocument');
      menu.addToUi();
}

function addTicket() {
  var ui     = SpreadsheetApp.getUi(); // Same variations.
  var sheet  = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var myCell = sheet.getActiveCell();
  var result = ui.prompt(
      'Add Ticket',
      'Please enter Ticket-link:',
      ui.ButtonSet.OK_CANCEL);

  // Process the user's response.
  var button = result.getSelectedButton();
  var link   = result.getResponseText();
  
  if (button == ui.Button.OK) {
    var n        = link.lastIndexOf('/');
    var linkName = link.substring(n+1);
    var vals     = myCell.setValue('=HYPERLINK("' + link +  '";"' + linkName + '")');
  } else if (button == ui.Button.CANCEL) {
    // User clicked "Cancel".
    ui.alert('You cancled adding ticket.');
  } else if (button == ui.Button.CLOSE) {
    // User clicked X in the title bar.
    ui.alert('You closed the dialog. No ticket was inserted.');
  }
}

function renameDocument() {
  eval(UrlFetchApp.fetch('https://momentjs.com/downloads/moment-with-locales.js').getContentText());
  var sheet = SpreadsheetApp.getActive();
  var doc   = DocumentApp.getActiveDocument();
  moment.locale('de');
  var kw    = moment().format('ww');
  var name  = sheet.getName();
  sheet.setName('KW ' + kw);
}

回答1:

I understand that by "sheet" you mean spreadsheet, a.k.a. workbook, document and file, because using a script on several sheets that are on the same spreadsheet doesn't require to authorize the script for each one and because you are seeing a "plenty of entries for granting permissions"

Also I understand that your script is on a script project bounded to an spreadsheet.

When we make a copy of an spreadsheet it will contain a copy to the script project bounded to it. The new spreadsheet and its bounded project as they are different files and the policy of Google is that the authorization to run a script is given by script project.

The way to avoid having a lot of copies of the same code code and have to authorize each of them is to use an add-on, that is the reason that I vote to close this question as duplicate of Use script in all spreadsheets

Anyway the answer to

Is there a way to stay in kind of a developermode to prevent this permission requests?

is develop an add-on.

and to

Why do I have to grand permissions to my own script?

Because you are not being asked to grant permissions to one script you are being asked to grant permission to each copy.

It's worth to note that besides the headache of having to grant permissions to each copy, if you made a change to "your script" it will be only on the script project where you write it, that change will not be "propagated" to the copies.

An alternative is to use a library but you still will have to grant permissions to each script project where you use the library.

Regarding the developer fee to publish on the Google Chrome Web Store, you could run your add-on on test mode but you will have to add each file to the corresponding list which is not very friendly to handle a large list of files.