CalendarApp.getCalendarById returns null - google-apps-script

We are using Google Apps Premier and I just noticed a change in behavior with the CalendarApp.getCalendarById() function, but it must have changed in the last month or so since it was working before the Christmas break.
The previous behavior (12/2012 and before) with the CalendarApp.getCalendarById() function used to be one where a calendar object was always returned as long as the calendar resource existed. Now this function returns null unless the the user has subscribed to the calendar resource. In the testing that I have done, as long as I am subscribed I get a calendar resource object and as soon as I unsubscribe, I start getting a null object. I have not tested with regular user calendars and have only tested with resources (such as rooms).
var calendar = CalendarApp.getCalendarById(calendarID);
This is pretty annoying since I need to run scripts against many rooms which I do not want to be subscribed to. The current work around of subscribing to a room or calendar is not very workable.

This has always been the case. You must have previously been subscribed and not realized it. I assure you this code was not changed.

Related

Looking For Solution to Automate Calendar Event Creation from Google Sheets

Background:
At my (new) job, we have a number of Sales Reps who book gigs for our organization. Until now, they handle all of their sales tasks in our CRM (Pipedrive [PD]). They then use that information to create an "activity" (event) on PD's calendar, which is set up for two-way sync with our general work Calendar (Outlook [OL]).
Problem:
Between the number of different Reps who edit the PD Calendar, the variety in bookings for our org, and just general human error, our Calendar system is a NIGHTMARE. Formatting inconsistencies, typos, lack of necessary information, and even straight up wrong details make my job (creating a program around, and then organizing the talent for each booking) nearly impossible.
"Genius" "Solution":
Even though it's a mess, I hate the type of person who comes into a new position and instantly tries to make changes to a system that has (somehow) worked before their arrival. So instead I sat down with the reps, learned what their needs were for the calendar, as well as their process, and built a system that not only gives me what I need, but also cuts the busy work on their end in half.
Previously, they not only entered into into PD, and then manually created the events, they also created three separate supplementary Word files that documented the details, created a detailed schedule of events, and outlined contractual needs based on the type of event.
So to solve all of this, I created a Spreadsheet (SS) Template that had all the fluff prefilled, was able to fill in all the contact info from an external contact database, and fill in the entire event schedule based on "type" and "start time". So basically they just need to enter in "Where", When", and "What", and the SS would auto populate the rest.
How that benefits me, is I then painstakingly scoured YouTube, Reddit, and Stack overflow for information on how to build Scripts for Google. And managed to make a function that can pull all the information from their new fancy form, and automatically create a 100% accurate and consistent Event for me.
Everybody wins, right?
New Problem:
Due to things well beyond my current knowledge, I am unable to create a dummy proof way for the event to "Trigger". I know it has to do with user permissions, and the limitations of Simple Triggers yada yada, but I'm incredibly annoyed that I managed to eliminate so much busy work, entirely solve my problem in the process, and yet the thing blocking me in the end is that I have to actively go to my Script and hit "Run" for it to properly function.
(For any "Expedition Force" readers out there, this feels a lot like how for so long Skippy could program intricate FTL Jumps across Spacetime, but still needed a "filthy monkey" to push the "jump" button)
Relevant Additional Details:
As I said, we use Office, but we're likely switching to Google down the road (and as someone who works in the arts and has been a poor student/struggling artist for the last two decades, I am much more versed with Google than Microsoft. I've also rarely found anything of value within my needs that one could do that the other couldn't, so I've built this whole system in Google Apps Script. However, if someone finds a potential solution where this will work in Excel with VBA instead, then I'd learn to translate what I've written so far.
Also, the Script works. I can make it work 100% of the time via the Apps Dashboard (where they don't worry as much about permissions), so it's not a problem with the code itself, which is why I haven't posted it here (but I can if anyone has a reason to think it's relevant).
It is a frequent need for our Reps to go in and edit the gig (people get sick, availabilities change, or mistakes were made the first time, etc.), so to combat this, I've actually written two Scripts. The first is a "Create" script which takes all the information, creates an event, and then pulls the EventID # which it pastes in a safe cell on a different tab. The second is an "Edit" script which searches for the previously created event by ID, and then makes the changes as needed.
In addition to PD and Office, we also use Monday.com and have a Zappier account. But I'd rather avoid either of those if possible since they have apparently changed software packages every other year before I got here, and we are in the middle of a search for a new Executive Director, so I'd rather not have this whole thing come crashing down because one minor part of it depended on Zappier, which our next ED cut from the budget, ya know?
Potential Solutions:
I can make the whole thing run via their Installable Triggers. However, I find them limiting:
I can do it by "Open" which creates the event every single time the SS is opened (which is both obnoxious AND useless, since the edits happen AFTER it is opened).
I can do it by "Edit" or by "Change", but again this just creates a whole new Event every time. I even tested it and made three random edits on empty cells in a useless tab, and it made three identical events to match the first (although this could be a solution for my 2nd Script...). I wish they could do onEdit of specific Cell, because THAT would be useful.
I can do it by form submission.. which means I can instead make a form that they fill out that creates the whole doc, and after they submit, it creates the doc AND event, but this limits the idea of having a different SS for every contract.
And lastly, which I really want to avoid, since I don't trust the Reps who manage to spell the same name wrong three different ways in three different places with having to open Scripts and hit "run", I could create a macro that notifies ME whenever changes are made, and then I could go in and manually hit the button. But..... Is that REALLY the best solution?
I know that's a lot, but I'm more looking for creative coding ways to solve a general problem, rather than a specific fix to a single string. Anything that could make what I want happen, without making me do annoying extra steps, or without requiring me to trust others with a keyboard, I am open to suggestions!
You would benefit from more research on triggers gerenally, onEdit in particular. It is broadly true that onEdit is triggered "when anything was edited" but the script can be written so that it evaluates specific rows &/or columns &/or cells &/or sheets &/or values. It can do this by using Event Objects which involve including an argument (often the letter "e", or the word "event"). The Event objects provide a lot of information about the nature of the edit.
For example, if you had a checkbox in sheet "Sheet2", column D, and you wanted to trigger something if the checkbox value was changed to "checked", then an onEdit(e) script might include an IF statement such as:
if(e.range.getSheet().getName() == "Sheet2" && e.range.columnStart == 4 && e.value == true){//do stuff}
In the scenario described, an automated trigger might not be necessary - a user might select a row (or a cell in a row), and click the "Button" in order to execute the script. However, it might be desirable to "check" a checkbox so that there is no doubt about that row has been calendarized or not. The advantage of this is that an event is not calendarized twice.
The following script might be an appropriate example:
function buttonTrigger() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheetName = "Sheet1"
var sheet = ss.getSheetByName(sheetName)
// get the row to be calendarized
var row = sheet.getCurrentCell().getRow()
// Logger.log("DEBUG: the cursor is on row is "+row)
// check is this row is already processed
if (sheet.getRange(row,4).getValue == true){
// this row already processed
// insert abend code
return
}
else{
// this row is OK to update
// insert script to calendarize event
// update checkbox to show event is updated
// checkbox is in Column 4
sheet.getRange(row,4).setValue("true")
}
}
Example
Creating a button and assigning a script
There is a good explanation of this process in StackOverflow topic Run script only on click on button instead with open the sheets

Google Apps Script for Vacation Calendar and Recurring Events

I am using the sample code from Google to populate vacation calendars.
It works rather well except I have recently noticed that it fails for recurring events. It throws error "Invalid iCalUID value" when trying to import a recurring or repeating event.
I believe this is due to the fact that, as per API documentation for 'iCalUID', "Note that the icalUID and the id are not identical and only one of them should be supplied at event creation time. One difference in their semantics is that in recurring events, all occurrences of one event have different ids while they all share the same icalUIDs." IE. it's probably due to duplicated 'icalUIDs' or similar.
There might be some way to modify/fix by using 'recurringEventId' and/or 'id' instead or in addition to 'iCalUID' but this be beyond my current time and skills. Has anyone solved the issue with this sample code already?

Gathering user data on a Google Add-on to determine usage levels appropriate for monetization

I have a Google Sheets Add-on about which I'd like to determine feasibility of monetizing. To do this I'd like to see frequency level of users; users per domain as well as how often they use the add-on.
I've read a bit about properties in the documentation, and I'm pretty sure that is at the heart of what I need to work on to start getting this data. My question is about routing that to a form I can use to crunch numbers.
I have considered just having the Properties Service send the user's email and date generated in the Add-on (not the current date, but the date for the content generated). Once that gets sent to a spreadsheet in my drive, I can use a script bound to the spreadsheet to determine if the user has been logged yet. If so, we move the date to the row for that user. If not, we create a new entry. I can also have the script order users based on domain so I can see domains with heavier usage.
I've never done this before, so I'm looking to see if I'm thinking in the right direction or if I'm overlooking something.

GAS - Access Other User's Calendar ID

I have a Google App for Business.
Is it possible to access other user's calendar to input & retrieve event?
I only know how to do it for my own calendar, but not others.
I have super administrator access.
Yes, you can get any calendar by it's ID, and then just get the events. As per your comment I don't believe that it will allow you to get any calendar in the domain, you appear to need to be subscribed. Whether this is expected or not I'm unsure.
Enter the Advanced Calendar Service, which uses the Calendar API to manage your domains calendars (and I suspect is probably the correct way to go about managing your domains calendars). After you turn the calendar service on, you can do what your looking for simply like so:
function calendar(){
var cal = Calendar.Calendars.get('calendar#domain');
Logger.log(cal.summary);
};
Which will return the 'name' of the calendar. (Note: For primary calendars, the 'name' of the calendar is almost always the same as the calendar ID).

Auto populating Google Calendar based on Google Sheets document

I've read a lot of posts related to my problem... The problem is I'm not a computer programmer and it's like reading a foreign language to me. I've tried to muddle my way through it but to no avail. I've mainly been working from this post and the links from that page.
I have a Google Sheets document with a list of id numbers and a start date and end date for treatments. I'd like to be able to run a script that will auto populate a Google Calendar with the id numbers as the event title and the treatment time frame. I don't need a start and end time, I'm only looking for dates.
I would also like to create another Google Calendar from the same spreadsheet. I have 6 columns associated with each id number - a projected date and an actual date for 3 different types of testing. I would like to be able to run another script that populates a Google Calendar with the type of testing as the event title. Again, not concerned with times at all, just the dates.
I'm using this to track an experiment I'm running and it's just a lot of information to see on a spreadsheet. It would be nice easier to see all the testing I need to do in a calendar format so I don't make any more mistakes in missing testing days or when to stop treatments.
I've been trying for 3 weeks to figure this out on my own. Let's just say, I know nothing about coding...
You should try writing a function in Google Apps script.
Get the active sheet, get range and retreive the id numbers and a start date and end date.
Then create a calender event by referring to this link.
Hope that helps!