The objective is to transfer data from Google Calendar to a Google Sheet whenever the calendar is edited. For this we need a script that transfers the data, and need to set up the Calendar trigger.
Thanks to Oleg Valter and his excellent and exhaustive explanation how to create the script on this page:
Google calendar events to Google spreadsheet automatic refresh onEdit
the data transfer already works fine when the script is started from the Google sheets. I use a modified version of Oleg's script, which works well, and I am not sure if it would make sense to re-post it here (since that is not the problem).
I have tried to create the trigger with Oleg's funtion first:
/**
* Installs Calendar trigger;
*/
function calendarTrigger() {
var trigger = ScriptApp.newTrigger('callback name here')
.forUserCalendar('calendar owners email here')
.onEventUpdated()
.create();
}
but the trigger did not fire when I have added a new event on the calendar. Checked the triggers connected to the project, and the trigger created by the above function was there. Checked the Executions and nothing showed up.
Then I have manually deleted the trigger, and manually crated a new one following the instructions under "Managing triggers manually" section of this page:
https://developers.google.com/apps-script/guides/triggers/installable#google_apps_triggers
The trigger was created again, which basically looked the same as the one created by the above function, but it still did not fire when tested.
Now I am stuck and have no idea what else to try. It supposed to work, but it is possible that I have missed setting up something else that might be required for this to work.
I am using a simple Google account connected to a single email address (not a business suite). Never had any problems with the other triggers related to the sheets before that I could not resolve (sometimes the platform refuses to work as it should, but then later things get back to normal).
I would highly appreciate any advice about what else to try, or if you know what the problem is, even better.
Thanks in advance,
Zoltan
Well, after some trial and error based on ziganotschka's kind answer (thanks again) found the problem. It is caused by a bit of confusion about what the
.forUserCalendar('calendar owners email here') and the
Calendar ID supposed to be.
On my Calendar account there are several calendars, but two are of interest for us here:
"Main Calendar" which is the default calendar associated with the account. If everything else is deleted, then this will remain.
another calendar that I have added for the very purpose to be connected to the spreadsheet, and named it "Auto Billing" (only this is checked and displayed).
The Main Calendar has this on the Settings page (changed my email address):
Integrate calendar
Calendar ID
myAddress#yahoo.com
The Auto Billing calendar has this on the
Settings page (changed the ID):
Integrate calendar
Calendar ID
svj8tu1o458a9s1488xxxxxxxx#group.calendar.google.com
My intent was to use a custom created calendar for this purpose instead of using the default Main Calendar, because this might be full with all kinds of events.
Therefore when the trigger is created manually
svj8tu1o458a9s1488xxxxxxxx#group.calendar.google.com
should be added in the field "Calendar owner email" instead of the calendar owner's email address.
When the trigger is created from script using the above snippet, then
.forUserCalendar("svj8tu1o458a9s1488xxxxxxxx#group.calendar.google.com")
should be used.
In the script that transfers the data from the Calendar to the sheet the
var Calendar = CalendarApp.getCalendarById("svj8tu1o458a9s1488xxxxxxxx#group.calendar.google.com");
should be used. This way the trigger will work when events are edited on the Auto Billing calendar.
Thanks for the guidance Ziganotschka, and also big thanks to Oleg for the original script. Hope this debugging will help others as well.
Google Calendar trigger is a feature for the Google Calendar
It does not work for "myaddress#yahoo.com" calendars
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'm using the Form Publisher add-on with Google Forms in order to turn each Forms response into a Google Doc. It's a great add-on.
I've written my own OnFormSubmit trigger (Head deployment; part of an app-script library) that renames the file(s) the form user may have uploaded as part of their form response. (The filenames are only available from the Drive.App file object, not from the form response itself.)
Based on the upload filenames that Form Publisher puts into the Google doc it generates, I can tell that sometimes the doc is being generated BEFORE my OnFormSubmit renames the files, and sometimes it is being generated AFTER my OnFormSubmit fires.
So my question is this: Given that I have no real knowledge of how Form Publisher works (though I would think OnFormSubmit would be at the heart of it), is there any way that I can assure that my OnFormSubmit will fire and finish before Form Publisher begins to generate the Google Doc?
Thanks, iansedano! I've continued to test and research, and everything I've seen points to your comment being correct.
I'm afraid that if you have two triggers that fire from the same event there is no way to ensure one starts/finishes before the other. They go off to the server asynchronously so the order will never be dependable. The way you normally deal with this is to make them part of the same trigger, but since you are mixing and matching your own script with closed source add-on script, that is not possible. If you wanted your one to fire after then you could just use a delay, but to make it fire before, you will probably be best off writing your own script.
For the record, there appears to be one small glimmer of hope here. I read that post's answer as saying that an add-on could use #customfunction to expose a function that my OnFormSubmit trigger could call; that custom function could then, for example, put the add-on to sleep for a bit (or even better: sleep until I call a second custom function to signal that I'm done) in order for my OnFormSubmit trigger to go first.
But other than that possibility, I've come up with nothing.
I want to have a Goggle Apps Script be run when a new Calendar Event is added to a Google Calendar. How can I setup the trigger? I know what I want to script (change color for an event based on the text in the Title). I just don't see where to setup the trigger to run my script.
You can use the new Google Calendar Event Triggers. You can create the code with a script similar to the following to trigger your function:
ScriptApp
.newTrigger('findNewEvents')
.forUserCalendar(Session.getActiveUser().getEmail())
.onEventUpdated()
.create()
Then you can loop through the events in your calendar to find any events recently created and pass them to your function.
Go to https://script.google.com/home/triggers (also available in your script editor, go to Edit -> All your triggers).
Create or edit the trigger for your script.
For event source use "From calendar", put "Calendar updated" to calendar details (see the image below) and specify the calendar owner email.
This feature is not supported yet as of now. You can see that no resolution has been provided in this issue tracker. You will have to rely on your own workaround implementation or open a new feature request.
My Triggers no longer work. I have two different google sheets with triggers setup. One sheet has a daily trigger that is set to fire between 9-10am. The other sheet has two triggers, one fires on a form submit and the second trigger fires on change. These had been working fine for a solid year before they up and stopped working a week or two ago.
For both of the google sheets, I have tried deleting and re-adding the triggers. For the one that has the daily timer, I have tried changing the time of day around also. No luck on them firing own their own. If I go to the Run Menu and run the function manually it works fine. I'd rather avoid having to create new google sheets as links to these sheets are published to users in my domain and are in production.
ie.
function updateScores()
{
Logger.log("Function started");
/*
all of my code here....
*/
}
When I manually run the function I see the "Function started" message in the logs along with all my other Logger.log statements I have throughout the code. Calculations are done correctly, emails sent, etc. The code itself works fine.
Actually, I've seen a couple of issues regarding triggers not working posted just recently in google-apps-script-issues tracker - Issue 6226 and the latest is Issue 6244.
As advised in this SO post - Google app script trigger not working, after checking the logs which you already have done and as you mentioned "The code itself works fine", you may post the issue in Apps Script Issue Tracker .
I would like to synchronize a google spreadsheet with a map so that I don't have to upload everything everyday.
I found that it's possible to synch a google form to google map using Google Fusion.
See, YouTube: Syncing Google Forms with Google Fusion Tables for Crowdsourced Maps.
But I couldn't replicate the process to my situation (I guess it's maybe because the spreadsheet content is not originated from a google form and maybe the script take that into account)
I don't know much about coding scripts but automating this process would be a blast for me!!
I hope someone will be able to help me out on this
thanks a lot and have a good day
The only thing to account for this situation is the difference of form submit. The guy in the video sets up two triggers: one for onFormSubmit, and one hourly trigger for syncing whenever any manual changes are made.
I haven't looked directly over the code, but all you should have to do is modify the onFormSubmit code and trigger. Change the code to look for and update the fusion table with any new rows from your spreadsheet. And then change the trigger to your desired need, timer would probably be the best option. So every hour, or day, or run it manually after your done adding rows.
Now, if you were to edit the rows of data after they've already been updated, the hourly syncing will take care of those changes.
I could imagine that the hourly sync method could be changed in such a manner to look for rows that need to be added, could be as simple as calling the submit function.
I had the same problem but i could solve it.
A time trigger is not needed if you set the sync function at the end of the function OnFormSubmit (so "sync();" under "insertRowId(rowId, row);" Syncing takes place after each sending of the form automatically.
For larger forms I found out that you should not make a special column Location in the Fusion table. The address column should marked as Location in Fusion table. In the script properties of the spreadsheet give the addressColumn the value of the column title of the address column and the third property keeps unchanged ("latlng">Location. What happens is that the value of the adres is overwritten by "latlng". So if you have trouble to loose the original addresses, add a new column, copy by apps script the same address (that piece of script direct at the beginning of the function OnFormSubmit) and (after syncing) give the addressColumn the value of the column title and in the Fusion table marks the original addess column as Text and the new column as Location.