How do I obtain the triggers for another document in my account?
There are two functions to obtain a list of triggers:
ScriptApp.getProjectTriggers()
ScriptApp.getUserTriggers(document|form|spreadsheet)
The first returns only the triggers for the current project (as expected), but the second is always returning an empty array, except when called from the project where the trigger resides.
There is currently a bug in the ScriptApp.getUserTriggers() function (or its documentation).
What is happening is not that empty arrays are always returned. Rather, the getUserTriggers() function is returning the triggers associated with the calling script, rather than the script bound to the specified document|spreadsheet|form. If the calling script has no triggers, getUserTriggers() will return an empty array.
The issue has been taken up by the Apps Script team. In the meantime, it is not possible to programmatically view or manipulate triggers of a script from another script.
Not a perfect solution but I've listed this issue on the script issues page at https://code.google.com/p/google-apps-script-issues/issues/detail?id=4562&thanks=4562&ts=1416775997, as no-one seems to know the answer, so we'll need to wait for the Google team to clarify.
Please star it so that it gains visibility and traction.
Edit: As per Ryans mention below, the issue I listed has been picked up the the Apps script team (Based on the owner being 'ryanr...#google.com', I suspect it's Ryan himself who has picked it up, Thanks Ryan!). So currently the only solution is to wait for a fix I believe.
Related
I’ve created a Google Sheet – kind of like an app – using script. It’s a number of blank cells where once added some information, it creates a string with that information in the right order. I created this to help some colleagues. We are using free accounts.
I face 2 problems:
The app is supposed to be used by only one person at a time, but I’m sure at some point two or more people will want to use it at the same time. Is there a way to allow this without they interfering with each other? I’ve read you can share a link that creates copies of your doc, but that wouldn’t work in this case because I intend to keep updating it regularly.
The changes people make to the sheet will stay there. Right now I have a onOpen function that just rewrites everything, but if someone accesses the document while someone is using it they’ll rewrite everything and ruin their work.
Any ideas on how to solve these two problems? Thank you so much in advance!
I think that you have to find another way to implement your "app" because Google Sheets not a good tool for limiting that only one user edit a spreadsheet a time, but if for any reason you decide to keep with it,...
... you need to implement a workflow like the following
Save the active user email on PropertiesService.getUserProperties().
This should be done by each user by running a script themselves. The most user-friendly will be by using a macro, custom menu or button (a drawing with an assigned Google Apps Script function). You might use an open installable trigger to validate that the active user have already done this.
You might have to make the function that clears the data a "smart" function.
You might have to consider additional cases, like if the owner of the spreadsheet opens it when there is an editor working on the spreadsheet.
Use installable triggers to manage the sharing settings.
Use an open trigger to remove all the editors except the active user
Use a time-driven trigger to add again the editors. To make this work effectively should define how the time-driven trigger will know that the last editor have finished their session, i.e. you might use DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId()).getLastUpdated()
The above should help you with both problems, as long you as the owner do not open the spreadsheet as is used by someone else.
Other alternatives that might work better is to create an add-on or a web application.
Related
Determine current user in Apps Script
Last modified date in Google App Script GAS sheets
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"
}
First time I'm working with google app scripts. I inherited a google sheet (template) from a colleague (who no longer works with us) that runs some scripts on change. The script from what I read should be a bound script.
When I open Script editor though, I see 2 projects. One is the copy of an earlier version of the project. Each of them has an onOpen() function. My question is how does the sheet know which one to invoke?
To replicate the behaviour, I created a sheet of my own and then using the script editor, created two projects each with the onOpen function. The function adds some menus on opening the sheet.
I see that both functions are fired, because the menu names are different. However, I don't see the same behaviour in the original template in question which seems to invoke only one of the two scripts.
I checked to see if there are any triggers etc. but none seem to exist. I find no documentation around this either.
They are both bounded to the sheet, as your tests showed both onOpen(e) are being invoked. What you could do to know which of the script was edited last or some details is to check the revision history of each script (in each script file-> see revision history).
I figure the last developer didn't comment the code so he must've disaled one of them by hand
It turns out as I mentioned that both onOpen functions were indeed being fired. Both scripts and tons of code in it but was exactly identical and the end effect was the same on the sheet. Pretty weird.
I fixed it by making a copy of the sheet which let me delete one of the projects and keep just one. I then deleted the original sheet and made my copy the official one (naming it the same).
I'd have preferred if apps script chose to make it unambiguous by mandating explicit triggers to a function on a particular sheet.
I've looked at the documentation for triggers, but couldn't find what I wanted. I essentially want to auto-fill certain spreadsheet cells when the cursor/selection moves to the cell.
For example, say I want any cell in column 'B' that is empty to be filled with the current time when it is highlighted.
How should I be doing that? I see Extending Google Sheets refers to UIService and HtmlService, but I didn't find anything about triggers for when current cell changes.
For your particular example, I would suggest creating a custom function Docs for Custom Functions (using apps script) which produces the current time and enters the value in the cell. It doesn't need parameters either. You could then call the function with something as simple as:
=time()
Add a few clicks for copy and paste, and you have a lot of functionality to play with.
Built in functions which produce the current time may or may not update as you reload the spreadsheet.
There are no triggers for the events you describe, and while you may post a request on the issue tracker, I would not expect to see such triggers implemented.
Using existing capabilities, the closest you could get would be a menu-driven function that fills the current cell with a timestamp. Hardly as automatic as you are looking for.
So, I have a bunch of Google Apps Scripts that use the onOpen/onInstall functions to load a menu or do some minor startup stuff. All of them have been working in the gallery for quite some time.
Recently, and I'm not exactly sure of the time on this, something Google has updated has caused a problem where:
OnOpen doesn't seem to always execute on open
OnInstall fires but doesn't have access to functions in the script. Specifically, if you try to select a menu item, Apps Scripts brings up an error saying the function does not exists (but it does)
There is an incredibly crappy workaround for the user: install the script twice. Somehow this gets the functions registered and the onInstall gets the menu loaded. But this only works until the sheet is closed; then they have to install again.
So here is my questions:
Are other people having this problem, have they figured out a workaround?
Is this something Google has acknowledged and is getting fixed?
Does anyone know when this happened?
Finally, I'd like to suggest the idea of "freezing" the api for a script in production. IE. we place a variable at the top saying we are using API version XXX and that is the API that fires out Apps Script. This is the third time I've had scripts in production that were then broken by a Google update. At some point we need to be able to know that when we put something out to the world that it will continue to work. Users don't make the distinction between our mistake and a bug in the API, which means that we take the heat from these sorts of problems.
Looks like a bug
It appears to be a bug with triggers attached to scripts that are loaded externally. One way to fix it is to manually add and remove an onOpen event trigger.
Note: You don't need to keep the trigger because the existence of an onOpen in the code will add the trigger implicitly.
I'm having the same issue with a script I released in the 'Script Gallery'.
As for the onInstall trigger, I think you're SOL until Google fixes it.