Using Google Apps Script Libraries

后端 未结 3 779
一向
一向 2021-01-13 19:05

I have read all Google documentation on managing and creating libraries, yet I still do not know if they are an appropriate option for the problem I am trying to solve.

相关标签:
3条回答
  • 2021-01-13 19:11

    besides making an add-on (already covered in another answer) I will answer your libraries question. They will work for you. What you are missing is the "connect" part.

    For this you want to trigger the library code from say, onOpen. The onOpen in the library is not enough and not detected by apps script. Instead each of your spreadsheet's script needs an onOpen(e) which just calls yourlibrary.onOpen(e).

    since those "hook" calls rarely change, specially once you stabilize your library api, and using it in "development" mode will let you modify just the library.

    whenever one of those hooks needs to change (say a callback from an html GUI needs a new parameter) you need to update all the spreadsheets. to avoid this, make all your callbacks receive a single json object instead of multiple parameters.

    0 讨论(0)
  • 2021-01-13 19:20

    Sorry if I am repeating other answers, but I would like to sum up and add something:

    You can access your library functions as follows:

    From the app using the library you go to the Resources/Libraries. You can see the library name under "Identifier". On the same line where you can select Development mode.

    Library name found in resources

    Now in your library you have for example a function

        function onOpen(e)
       {
         Browser.msgBox("HELLO!"); 
       }
    

    In the spreadsheet app you wish to access it you use the library name found in the resources, for example "testlibrary"

    function onOpen(e)
    {
      testlibrary.onOpen(e);
    }
    

    Now if you have development mode on, the modifications to functions in the library update automatically to your application (spreadsheet) as long as the user using the application has edit access in your library script.

    If anyone using your spreadsheet has a restricted access to your library script (meaning only view access) or development selection is off in the application, you have to go to the application's script, Resources/Libraries and select the most recent version of your library to be used in the app everytime you update the library and save a new version of it.

    Still, especially if you are using mostly only the onOpen function , I would recommend using the library rather than copy-pasting the function to the script of each spreadsheet, as it is easier to track which scripts are up to date and it is easier to avoid errors and differences between the scripts.

    Even in the more restricted case, if you update function in library - as long as you are already calling it in the app - all you have to do is select the new version of the library used.

    I hope I had anything to give in this conversation and my language was appropriate, this was my first answer..

    0 讨论(0)
  • 2021-01-13 19:35

    A good question Melly. I've been through a bunch of the documentation and some tutorials on this subject but haven't tried adding any libraries yet. My understanding is that once you are connected to a library all you have to do is call the applicable functions. That once a library is connected the functions in the library become an extension of all the other Apps Script classes available in the script editor.

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