Custom formula returning #NAME when Google Sheets is published to web - google-apps-script

I'm not much of a programmer, but I managed to write a Google Sheets script that sorts an array, filters out specified values, and returns the new array. It works great.
However, when I publish the output page to the web, the link will only successfully show the output if I have the back end sheet open. As soon as I close it, the publish page turns up #NAME.
Presumably there is some restriction as to the availability of custom scripts. I assumed the output would just grab the text, but I suppose it's trying to run it and can't without someone being logged in?
Anyways - is there some way around this?
Thanks
Edit: Did a little further reading and found out that this might be a situation where "script as web app" might be the requirement. As a novice, this is a little over my head and I'd appreciate any advice.
I also tested to make sure IMPORTRANGE() as a source for this data wasn't the issue, and though it may cause other problems once I get the #name issue resolved, it does not appear to be at fault here specifically since the same thing happens with local data.

Yes, as you've stated, script as web app might be the requirement. You may also want to check this documentation regarding Custom Functions in Google Sheets. Just be noted that a script can be published as a web app if it meets these requirements:
It contains a doGet(e) or doPost(e) function.
The function returns an HTML service HtmlOutput object or a Content service TextOutput object.
Hope this helps!

Related

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 Apps Script, calling function once when document is copied

I have been playing around with Google Script Editor and I've gotten heaps of use out of it.
The next task that I am looking at is automatic formatting and text insertion/replacement when copying a Template.
An example use case is as follows: Within my organisation I have submitted a Doc to the template gallery. When creating a copy of the template I want it to automatically insert today's date and the current time (rounded to the nearest hour).
This is a question about the Triggers. The text replacement bit is easy and done. Not to mention this is just one of the basic use cases, I'll be attempting many more similar behaviours with things like timesheets and the like.
The problem that I am running into is that I can't seem to get the triggers to work as I'd like them to.
2 of the Triggers that I thought I could try and use: onOpen(e) and onInstall(e).
onOpen(e), though it works, it works "too well". That is, it also replaces the text on the original template as well, proving a nuisance when updating info in these templates.
onInstall(e), I thought this would work as creating a copy of the Doc also "installs" the script as well. However this function doesn't seem to run at all.
Any ideas about getting a trigger to happen once and only once when a Doc is created from a template?
Cheers,
Bricktron
First of all, Trigger onInstall(e) works only for Add-ons.
Now coming to onOpen(e), in my opinion you can use Google Apps Script Property's Services to store one flag which helps your code identifying whether this file has been opened or not.
So for very first time onOpen(e) runs, assign property eg: propertyService.setProperty("opened","TRUE") and next time you can check by accessing the property whether it has been already "opened" or not.
Example:
var openedFlag=propertyService.getProperty("opened");
if(openedFlag=="TRUE"){
//Document has been modified
//Do not run the modifiable code again
}else {
//First time
//Edit the file
//Set the propertyService to "TRUE"
}

How can I let others install my Google Apps Script?

I've made a simple script in Google Apps Script. The script builds forms based on a spreadsheet, the script itself is part of the spreadsheet. It adds a menu item. People would like to use it. They are non-technical people, so I would like to make the installation of my script as easy as possible. With 'installation' I mean a way to 'import' my script into their spreadsheet and automatically set up the needed triggers so the menu item becomes visible. It should be possible for anyone to install it.
I've been thinking of the following solutions:
Publishing as an add-on: my script wouldn't qualify, and even then I can't wait for Google's approval.
Copy-paste. I don't like this, it's way too complicated.
I've looked at this question: https://stackoverflow.com/questions/5334751/how-do-i-share-a-script-i-wrote-with-my-co-workers-on-the-same-google-apps-accou, but the people wanting to install it are not on the same domain in my case. The question is also 3 years old.
EDIT: As it turns out, there doesn't seem to be a solution. I've filed a feature request here: https://code.google.com/p/google-apps-script-issues/issues/detail?id=4122&thanks=4122&ts=1403949074
If your Google Account administrator allows you, you can share scripts/spreadsheets outside your organisation either by sharing directly with another person with a Google Account (Gmail or Google Apps) or by making it public (share with a link or published online).
https://support.google.com/drive/answer/2494822?hl=en
Keep in mind that if you use the Script Properties to store values, each time someone uses the shared script it will overwrite the values. In a shared context it is best to use User Properties instead. This way each user can store their own values.
https://developers.google.com/apps-script/guides/properties
There is no way to import a script in an existing spreadsheet , the "normal" workflow is , in a way, the reverse process, that's to say create a sheet from an existing template that already has the script in it and start to work with that copy.
From your description I'm not sure this method could be useable but I'm afraid there is no other way if you want people who will be using it should not go into the script editor at all.
There would be a couple of functions to write that should run at install time and would create the triggers (if needed) and trigger the authorization process.
I made such a SS some time ago that needed authorizations and I added an "install" menu that made its use quite simple (example here on a friendly hosting site).
I know this is probably not the answer you were expecting but it was definitely too long to fit in a comment anyway.

How to share one Google Apps script between few documents?

I wrote short script for numbering of document sections. But every time when I want to use it in new document I must create new copy of that. I tryed to publish the script by option "Deploy as web app..." but it is not clear how to connect it in new document. Is it possible? I have few documents in Google Drive and few copies of same script for each of document. Can I connect every document to one script? Thanks a lot!
This is not possible for now, there is an open enhancement request that you could star to mark your interest and be informed if something new comes up...
I had a similar problem.
The leaner solution that I was able to imagine is to keep the function in a saparate, shared script file. In the spreadsheet script, you will use the shared script file as a library.
In this way, your logic remains in a single copy, the actual part of the logic that is copied several time is only a call to a shared function.

AppScript in SpreadSheet and AppScript in WebApp

Just need to clarify something and ask for a reference.
App Script Execute within the spreadsheet works fine with all the example.
but.
when appscript is deployed as webapp using scriptEditor -> publish -> Deploy as Webapp
most of the code in appscript is not working.
ex: using script editor create a form using builder create a doGet() function and show the form bind some function to button?
can anyone point me to a reference like opening the spreadsheet where the form is within and manipulating cell values.
can you please give me information where can i get reference for scripting on forms as webapps.
Thanks
The main difference between these 2 modes when accessing a spreadsheet is that the web app does not know the spreadsheet it is linked to unless you tell it explicitly, there is no such thing as 'active spreadsheet' or 'active sheet'.
So you should open the spreadsheet by ID (openById('long ID number') and choose the sheet by its name or index.
The other point, but I guess this one you'll find it obvious, is that UI functions attached to spreadsheet must end by "var doc = SpreadsheetApp.getActive();doc.show(app);" and web apps only by "return".
There are many examples on the web, some are available on Google Developpers or also here , hope you'll find some interesting ideas.
There are few more difference you might get in webapp like when you use it u might not able to use some classes that are limited to appscript only like: when you creating a page u cannot use Logger.log() instead you can use console.log() and see logs by inspect element > console > bottom.
web app allow us to use html, css, javascript to build web pages