How can I invoke my standalone script in my spreadsheet?

后端 未结 3 533
野的像风
野的像风 2020-12-30 11:30

I have created a standalone Apps Script from Google Drive, but when I try to access it from a Google Spreadsheet, I don\'t see a way to access the script (even though when I

相关标签:
3条回答
  • 2020-12-30 11:50

    The behavior you describe is exactly how it is intended to work... the script you wrote can only work if it is bounded to a spreadsheet.

    The fact that you wrote it in an independent script makes it indeed a standalone app but it can only be executed from within the script editor and in this context an onOpen function like the one you wrote doesn't make sense (it runs on opening a document/spreadsheet, not the script).

    For now a script can only be bounded to a spreadsheet by copy/pasting the code, at least a script that you want to use as you do (with a menu and direct function calls and activeSheet calls), maybe one day Google will expand the concept of libraries to make your script function available from different document directly (read also the doc about libraries).

    There are actually 2 types of standalone Apps Scripts, the one you tried that runs only from within the editor and webapps that are deployed and run in a browser window by themselves. The latter are always build around a doGet function that represents an entry point for the application. You should read the documentation on the subject to get all the details.

    To summarize : if you want to access your spreadsheet using getActiveSpreadsheet() and similar methods then you have to write (or paste) the script in the script editor bounded to the spreadsheet.

    If you want to have a standalone script you can also interact with spreadsheets (and other document as well) but you have to access them by their ID and the spreadsheet will only be viewable from another browser window, without link between them, and there will be no "active spreadsheet" nor "active sheet"...

    A webapp can of course also do that with the advantage of a user interface that you can adapt to your needs but this is becoming a bit too far from the scope of your question.

    Hoping I've made it (a bit) more clear, at least I tried to make it simple ;-)

    0 讨论(0)
  • 2020-12-30 11:54

    I worked this out with Jordan, following the instructions here:

    https://developers.google.com/apps-script/guide_libraries

    • First, he had to share his standalone script with me, so I had read access to it (probably setting the script as public read would work too).
    • Then he told me his project key.
    • I took his project key and inserted it in "Find a Library", in the "Manage Libraries" menu.
    • For this to work, Jordan had to export "a version" of his library - so third parties can rely on stable code and upgrade at their own rhythm.
    • As Jordan's script runs automatically on onOpen(), my code had to define a function onOpen() that called his script onOpen(). To do this, the library manager gave me an identifier for the imported library.

    Sample code once the library is imported and the library manager gives you an identifier for the library:

       function onOpen() {
         GivenLibraryIdentifier.onOpen();
       }
    
    0 讨论(0)
  • 2020-12-30 11:59

    You can also use installable triggers. If you install an installable trigger (such as onOpen) from your standalone script into the document, it gets similar privileges as if it were a simple trigger.

    And better yet, when the installable trigger calls its function from your standalone script, it executes WITHIN your standalone script! So it can call any other function in your standalone script. Its Logger.log() entries can even be viewed from your standalone script, even though it was executed from your sheets document.

    So basically, if your script installs an onOpen() trigger tied to one of its functions, it's as good as if your Sheets document had required() your whole standalone script file.

    (Assuming the owner of the script has edit privileges to the file.)

    0 讨论(0)
提交回复
热议问题