Google Sheets to link to Google Team Calendar using Script Editor - google-apps-script

Hi I've been trying to experiment with google's script editor and i think that I've gotten the code right but it wont seem to work. I used the code they made in google's tutorial video with edits but it cant seem to create an event. On the executions page it says completed but no new event is showing up.
Anyway i've attached screen shots of the sheet as well as the code. Hope y'all can help thanks!
function scheduleMeetings() {
var spreadsheet = SpreadsheetApp.getActiveSheet();
var calendarId = spreadsheet.getRange('H6').getValue();
var eventCal = CalendarApp.getCalendarById(calendarId);
var tasks = spreadsheet.getRange("G10:H100").getValue();
for (x=0; x<tasks.length; x++) {
var schedules = tasks[x];
var date = schedules[0];
var task = schedules[1];
eventCal.createAllDayEvent(task, date);
}
}

Try this:
function scheduleMeetings() {
var spreadsheet = SpreadsheetApp.getActiveSheet();
var calendarId = spreadsheet.getRange('H6').getValue();
var eventCal = CalendarApp.getCalendarById(calendarId);
var tasks = spreadsheet.getRange("G10:H100").getValues();//you had getValue()
for (var x=0;x<tasks.length;x++) {
var schedules = tasks[x];//Select a row
var date = schedules[0];//column G
var task = schedules[1];//column H
//you may need to add var date=new Date(schedules[0]);
eventCal.createAllDayEvent(task, date);
}
}

Related

Trying to create an automation from google sheets to google calendar while avoiding duplicate event entries

I am very new at this and totally lost. I am trying to create a spreadsheet to use in my business with scheduling information that I could automatically send to google calendars to create an event. I would like to be able to add a trigger to the spreadsheet to update this spreadsheet manually as to capture new events and add them to google calendar as well without having to erase the previous information. In other words, I would like to have it just add new content or if it works better, delete all events and recreate events each time it runs. I have come up with the basics, but I have been having a few issues.
1st issue I am having is I am receiving the following error:
Error
Exception: The parameters (String,String,String,(class)) don't match the method signature for CalendarApp.Calendar.createEvent.
addEvents # Scheduling.gs:38
2nd issue I am having is:
Even with the error above, the script does run, however, each time it runs it creates a new event leaving me with several duplicated events.
Would anyone be able to help a very very code illiterate person with how to fix this?
Here is a copy of the script I have:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var cal = CalendarApp.getOwnedCalendarById("calendaridhere");
var events = cal.getEvents(new Date("07/01/2022"), new Date("07/31/2022"));
var lr = ss.getLastRow();
for(var i = 0;i<events.length;i++){
var title = scheduling[i].getTitle();
var sd = scheduling[i].getStartTime();
var ed = scheduling[i].getEndTime();
var loc = scheduling[i].getDescription();
ss.getRange(i+2, 1).setValue(title);
ss.getRange(i+2, 2).setValue(sd)
ss.getRange(i+2, 2).setNumberFormat("mm/dd/yyyy");
ss.getRange(i+2, 3).setValue(ed);
ss.getRange(i+2, 3).setNumberFormat("mm/dd/yyyy");
ss.getRange(i+2, 4).setValue(loc);
ss.getRange(i+2, 5).setValue(des);
}
}
function addEvents(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();
var cal = CalendarApp.getOwnedCalendarById("scheduling.ohiolegacy#gmail.com");
var data = ss.getRange("A2:E" + lr).getValues();
for (var i = 0;i<data.length;i++){
cal.createEvent (data[i][0], data[i][1], data[i][2], {location:data[i][3]});
}
}
My spreadsheet is very basic, but here is an example:spreadsheetexample
Any help or suggestions would be GREATLY appreciated! Please though, I am new and honestly need a lot of guidance to understand what I am doing.
Thank you!
Try
function addEvents() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();
var cal = CalendarApp.getOwnedCalendarById("########gmail.com");
var data = ss.getRange("A2:E" + lr).getValues();
for (var i = 0; i < data.length; i++) {
if (data[i][4] == ''){
var d = new Date(data[i][2].getFullYear(),data[i][2].getMonth(),data[i][2].getDate() + 1)
ss.getRange('E' + (+i + 2)).setValue(cal.createEvent(data[i][0], data[i][1], d, { location: data[i][3] }).getId())
};
}
}
the event id will be stored in column E if free
check if columns C and D are dates (and not strings)
this will also includes the end date inside the calendar

How to directly open the new google sheet created from script?

I have been using the following code (after some edits and help from the community here) to create a duplicate sheet from a template sheet. However, the number of sheets have increased now. Is there a way to make some edits in this code such that the newly created sheet shows up directly so I can right away edit without horizontally scrolling through to find the new sheet that was created from the script? .
Edited question for giving better clarity on the issue.
function duplicateSheetWithProtections() {
var ui = SpreadsheetApp.getUi();
var oldSheetName = ui.prompt('Sheet to Copy?');
var newSheetName = ui.prompt('New Sheet Name?');
var ss = SpreadsheetApp.getActiveSpreadsheet();
sheet = ss.getSheetByName(oldSheetName.getResponseText());
sheet2 = sheet.copyTo(ss).setName(newSheetName.getResponseText());
var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
var p = protections[i];
var rangeNotation = p.getRange().getA1Notation();
var p2 = sheet2.getRange(rangeNotation).protect();
p2.setDescription(p.getDescription());
p2.setWarningOnly(p.isWarningOnly());
if (!p.isWarningOnly()) {
p2.removeEditors(p2.getEditors());
p2.addEditors(p.getEditors());
// p2.setDomainEdit(p.canDomainEdit()); // only if using an Apps domain
/* Make the new sheet active */
ss.setActiveSheet(sheet);
}
}
}
Try
sheet2.moveActiveSheet(0)
this will be the simplest and most effective solution

Google Spreadsheet: loop through sheets stop working

i've a function called when opening my spreadsheet that aim to loop trought all sheets, activate them and set cursor to the last modified cell (saved by another function) and, a the last re-select the last used sheet (saved as before by another function).
It worked fine for years, but suddenly stopped working.
Here is my code:
function onOpen()
{
var timer = 2000;
var proprieta = PropertiesService.getScriptProperties();
var lastSheet = proprieta.getProperty("ultimo");
var lastModifiedCell = proprieta.getProperty(lastSheet);
var applicazione = SpreadsheetApp.getActive();
var fogli = applicazione.getSheets();
var ui = SpreadsheetApp.getUi();
for (var i=0; i < fogli.length; i++)
{
var attuale = fogli[i].getName();
var ultimorange = proprieta.getProperty(attuale);
applicazione.getSheetByName(attuale).getRange(ultimorange).activate();
//ui.alert(attuale);
//ui.alert(ultimorange);
Utilities.sleep(timer);
}
applicazione.getSheetByName(lastSheet).getRange(lastModifiedCell).activate();
}
Doesn't return any error but the for loop is not selecting anymore sheet and range given.
If i enable the ui.alert the sheet name and range are displayed correctly.
The last selection ath the end, works correctly...i have no idea why it happens...
Is there anyone who can help me figure out what's wrong?
Thanks to anyone who will answer!!!
I was able to replicate the issue. To prevent this from happening, you can use SpreadsheetApp.flush()
Your code:
function onOpen()
{
var timer = 2000;
var proprieta = PropertiesService.getScriptProperties();
var lastSheet = proprieta.getProperty("ultimo");
var lastModifiedCell = proprieta.getProperty(lastSheet);
var applicazione = SpreadsheetApp.getActive();
var fogli = applicazione.getSheets();
var ui = SpreadsheetApp.getUi();
for (var i=0; i < fogli.length; i++)
{
var attuale = fogli[i].getName();
var ultimorange = proprieta.getProperty(attuale);
var range = applicazione.getSheetByName(attuale).getRange(ultimorange).activate();
SpreadsheetApp.flush();
Utilities.sleep(timer);
}
applicazione.getSheetByName(lastSheet).getRange(lastModifiedCell).activate();
ui.alert("Activation Done");
}
I'm new but i found something recently (i come from excel scripts) :
getting a "range" and getting it's a1Notation are totally different.
Maybe your "ultimorange" (from "attuale") is a range and NOT the notation of the range. It would make getRange(ultimorange) not working.

Google App Script: get values from range and set them to another range in different sheet

I did spend some time to find solution for this, but in vain.
I am a beginner to google appScript.
I have list of values in sheet1(ASINs to review) from (B5:B60) and its dynamic(number of rows change due to query on the sheet). I am trying to pro-grammatically pull them and try to paste them in sheet2(app).
I am able to successfully pull the data, but unable to paste them in sheet2.
My code:
function myFunction() {
var asinsToReview = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ASINs to Review");
var lr = asinsToReview.getLastRow();
var title = asinsToReview.getRange(5, 2, lr).getValues();
var pasteSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("app");
var pasteRange = pasteSheet.getRange("A2:A").setValue(title);
}
Something like this should work:
function myFunction() {
var asinsToReview = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ASINs to Review");
var lr = asinsToReview.getLastRow();
var title = asinsToReview.getRange(5, 2, lr).getValues();
var pasteSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("app");
var pasteRange = pasteSheet.getRange(2,1,lr,1).setValues(title);
}

Google Script - Create Events to Multiple Calendars

I have a spreadsheet with a list of events, but need to insert them into their own respective calendars. So far I have this:
function pushCalendars() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
for (i in sheets){
if (sheets.length > 1){
var lastRow = sheets[i].getLastRow();
var range = sheets[i].getRange(3,1,lastRow,100);
var data = range.getValues();
for (i in data){
var row = data[i];
var title = row[0];
var title2 = row[1];
var date = row[2];
var desc = row[3];
var calName = ss.getSheetByName("Resorts").getRange('E3').getValue();
var cal = CalendarApp.openByName(calName);
cal.createAllDayEvent(title,date);
}
}
}
Let me know if you have any solutions!
If the calendars are owned by your user you will face no problems.
The only thing that you need to change in your code is to create the calendars that still does not exists with this piece of code:
// Creates a new calendar named "Travel Plans" with a summary and color.
var calendar = CalendarApp.createCalendar('Travel Plans', {
summary: 'A calendar to plan my travel schedule.',
color: CalendarApp.Color.BLUE
});
Logger.log('Created the calendar "%s", with the ID "%s".',
calendar.getName(), calendar.getId());
The API documentation can be found here.
If the calendars belongs to other user you will need to have administrative permissions to modify it and you need to log with OAuth plus calling the calendar API using URLFetch (Calendar API can be found here.)
I always get my calendars by id, since the calendars already exist, you could get the calendar ID from the Calendar Settings, and insert them into the script as Project Properties. Then all you need to do is load the id that matches the name.
Change these two lines:
var calName = ss.getSheetByName("Resorts").getRange('E3').getValue();
var cal = CalendarApp.openByName(calName);
To this:
var calName = ss.getSheetByName("Resorts").getRange('E3').getValue();
var calID = ScriptProperties.getProperty(calName);
var cal = CalendarApp.getCalendarByID(calID);
https://developers.google.com/apps-script/guides/properties#scriptProperties for reference on ScriptProperties.