Sharing a document doesn't share the script? - google-apps-script

I have a Google Sheets document with a bunch of script attached. I view and edit that script using Tools > Script Editor. This script also adds a new menu to the toolbar using the onOpen function.
If I share the document with a colleague, she sees the new menu and can run scripts in that menu, but Tools > Script Editor is disabled for her.
I'd like anyone with access to the document to also see the script. Do I need to create an add-on or something instead?

As documented in Google reference documentation, document bounded scripts inherit the sharing properties from their container.
(A few years ago it was not the case but even very old documents have all been updated).
If the behavior you describe can be repeatedly reproduced then you are most probably facing a bug and should report it to the issue tracker.
In the mean time try to make a copy of it and share it with the same user to see what happens.
A similar issue occured to me a while ago and unsharing/resharing the doc solved the issue. Worth trying...

Related

How do I build a Google Add On with a menu item that pops up in all documents?

I'm new to Google Apps Script, so forgive me if I use the wrong terminology. I'm trying to build a Google Apps Add-On but I'm having trouble piecing things together. Ideally I'd like to add a right-click menu to all documents when someone installs my Add-On. Similarly to how LucidChart adds a top level menu to all docs.
https://developers.google.com/apps-script/reference/base/ui#createAddonMenu() says:
If the script is running as an add-on, the sub-menu name matches the add-on's name in the web store; if the script is bound to the document directly, the sub-menu name matches the script's name.
Should this work in an Add On:
function onOpen() {
DocumentApp.getUi().createAddonMenu().addItem("Test", "test").addToUi()
}
function test() {
DocumentApp.getUi.alert("Yeah!")
}
and if so, what am I supposed to do after doing this to trigger onOpen on all the documents I open in my account? I mean, at the moment, just to test this add on (that is, to start developing it). I'm not sure what to do to run it.
You should create a unbound/standalone script when creating a addon. But it'll become bound to the document, when a user installs the addon on that document. See Authorization modes.
I'm piecing together an answer. There are currently two editors for Google Scripts, the current one and the legacy one. I still don't know if it's possible to test an add-on with the current one, but it seems possible in the legacy one.
To switch from the current one to the legacy one, there's a link on the top right:
Once in the old editor there's "Run" menu:
that has a "Test as add-on" function:
That opens a dialog that allows you to open another document with the add-on installed, enabled, or both:
Before finding this out, I enabled showing the script manifest, added the "addOn" entry, and deployed as a Test Add On in the new editor. I'm not sure if that had an effect.

Is it possible to add the same custom menu to every new google document? The goal is to make a local script function like an installed app

Is it possible to add the same custom menu to every new google document? The goal is to make a local script function like an installed app.
This seems like it should be easy but I can't seem to find a way to do it. I'm using a standard account for personal use. I've created a script attached to the original document and also made copy as a stand alone script. (I'm not sure that makes a difference.) I've searched and read about deployment and it seems to be only useful for a domain account or publicly as an app. I've played a little with scripting but this is the first attempt to get a script working "globally".
Any help will be most appreciated.
I found a way to do this in an old example at: https://sites.google.com/site/scriptsexamples/custom-methods/2d-arrays-library.
The script is in a project and that needs to be connected to the current doc. The onOpen() script is a UI that contains the line:
.addItem('Select Phrase', 'PS.showPrompt')
where PS is the name of the attached library. Once this is done, as you said, you can make a copy and use it with the script active.
It is a little slow as they describe about using a library, but is make the code easier to maintain. Thanks to all for the direction to look.
What we do is create a copy of the original document (the document with the attached google apps script). The script is copied along with it and the copied document has the exact same functionality (menus, custom functions, etc).
You could create a Google Docs Add-On using an independent Apps Script script and then install your Add-On globally. For more information regarding its installation check this documentation. However you would need to run this Add-On on the new documents to get your desired menu.

Google Sheets Script not sticky

I am trying to create a custom function for a Google Sheet. to do this, I start from within the Sheet that I want to use the function, I then go to the menu Tools|<>Script Editor.
I then write a function. Name the file. And save it.
But, I can not use this function. Error #NAME?
Then if I close the Script Editor and open it again, it's like I have never been there. It presents the same form you see when you first start.
From an OP comment
I have discovered the issue, but not the solution. When the script editor is opened it switches me to another Google account I have. So I switch back. But then I am presented within a screen saying Acess Denied and making me ask permission. To whom and what for I am not sure.
The solution to the root cause of the problem depends on Google, fortunately there are several workarounds.
Use Chrome in incognito mode with all the extensions disabled, then sign in on only one account (the key part is bold)
Use another web browser or web browser profile. Sign in on only one account (the key part is bold)
Signout of all your Google accounts, then sign in first on the account that you will be using for developing your scripts, then sign in on the other accounts. This still migth be problematic under certain circunstances.
Also, as good habit, when you click Tools > Open script editor to create a new bounded project, the first thing that you should do is give a name to your project.
Another god habit, before testing a custom function, be sure that there isn't a project file with a red asterisk (unsaved file). Contrary to spreadsheets, the Apps Script files aren't automacatily saved when a edit is made.

Standalone Script for Custom Functions [duplicate]

This question already has an answer here:
Custom Functions with add-ons?
(1 answer)
Closed 1 year ago.
I am developing an add-on with custom functions. This also includes a sidebar.
When I test the addon, I do the authorizations and the sidebar functions as it should.
I am unable, however, to use the custom functions - they just don't appear when I start typing. I know they should work ok, since I prepared them in a bounded script originally and they were fine there. Then copy & paste to the standalone script, but no joy.
There are 3 OAuth scopes required by the script:
https://mail.google.com/
https://www.googleapis.com/auth/script.container.ui
https://www.googleapis.com/auth/spreadsheets
I then updated the appsscript.json file to include the scopes above, as suggested here:
Google Setting Scopes
I'm assuming this has something to do with authorizations. What should I be looking for? Any suggestions please?
I think you are experiencing the following issue, reported in Issue Tracker some time ago:
Issue #36763437. "Test as add-on" from standalone project to Google Sheets doesn't link custom functions
Custom functions don't seem to work when the add-on is being tested with Test as add-on. Crucially, this problem only seems to show up while in test mode: when the add-on is published, custom functions do show up.
Anyone you wants to keep track of this can click the star on the top left in the referenced page.
In any case, since the rest of the add-on actions seem to be working correctly, most probably the custom functions will work after the add-on is published. I understand, though, that not being able to test the add-on before publishing can be frustrating.

Sharing Google Docs content-bounded scripts

I have created some content bounded scripts (Tools->Script Editor) for private use. The scripts add a sidebar, and use a timer to copy the content from the sidebar to a Google document at the cursor position. It is important that the insertion happens at the cursor.
I want to share these scripts with others, as well as add them to old documents. All suggestions I have seen so far involves creating a template document with these scripts and then sharing the template document. This option is not feasible for me. One of the concerns is, these scripts need to be added to existing documents. Creating a new document, and merging it with the old one is not working (the original documents are complex, and migrated documents loose formatting).
Is there an easy way to share/insert these scripts? So far, I have failed to implement any of the below obvious options:
Export Script Editor project to somewhere, and then import it in another document.
Create an app script in drive, and then import it in an existing document.
Publish this as add-on (N/A since this is not a public project, and not complete yet either).
I'm not sure if it is feasible to publish my scripts as a webapp, then write a simple container-bound application to call my webapp with document id as suggested at Deploying container-bound Google Apps Script as Web App
However, this requires webapp to insert the text at cursor position of the active window. I doubt that is possible.
I'm hoping that somebody found an easier way, and willing to share it.
I appreciate any help.
Sincerely,
Converting your script to an add-on is the recommended way to distribute across multiple users and documents. We understand this solution doesn't always work when the script is not meant for general consumption. Add-ons can be published only to a single domain however, so if all of your users are within a Google Apps domain then add-ons may still be an option.