简体   繁体   中英

When Google document is copied with container-bound script, how to get its installable triggers?

Issue

I have a container-bound script attached to my document. It has onOpen installable trigger and I need to get the script in newly created documents. The most effective way I found is to create the new documents by copying from the original document.

The problem is, that the triggers are not copied with it.

Conditions:

  1. Simple triggers are not enough, I need to use action requiring authorization
  2. I don't want to force my users to create manually trigger for each document
  3. I don't mind if the installable trigger is created after clicking button from the programmatically created menu

My thoughts:

I wanted to create it programmatically – there is a problem with the testing environment. I am getting an error:

The add-on attempted an action that is not permitted in Test as add-on mode. To use this action, you must deploy the add-on.

as I understood, I need to release the project to the store to use this, which I don't want to do.

I don't mind release as an add-on, but the google script IDE only offers to release as web Docs web add-on.

Code:

function onOpen(e){
    DocumentApp.getUi() // Or DocumentApp, SlidesApp, or FormApp.
        .createMenu('Custom menu')
        .addItem('Open sidebar automatically', 'createTrigger')
        .addToUi();
}


function createTrigger() {
    var doc = DocumentApp.getActiveDocument();
    ScriptApp.newTrigger('onOpenReal')
        .forDocument(doc)
        .onOpen()
        .create();
}

function onOpenReal(e){
    ...something requiring authorization...
}

I figured the answer partially:

I think it is not possible to copy the document together with its triggers, but I didn't find any official documentation about that.

But the error:

The add-on attempted an action that is not permitted in Test as add-on mode. To use this action, you must deploy the add-on.

in the seemingly regular environment was caused by saved test configuration in the Google apps script editor.

To get rid of the error you need to first delete all test configurations in "Test as add-on" option. Until that the system sees all launches as "test" even if opened as a regular user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM