Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

Google calendar sync with google spreadsheet

I've found this pretty awesome script that syncs btw google cal and google spreadsheet. Two-way sync. And it works! -> https://github.com/Davepar/gcalendarsync

Now I wan't to be able to put in the address of the calendar in a sheet named Data. In cell B1. The script should pull the address from there so that I wouldn't need to change it in the script.

Can someone figure out how to do that?
The changes would involve to define calendarId by Data!B1
And the script should only run on the Sheet

You can see sample doc here: https://docs.google.com/spreadsheets/d/1TCIIBRmshx2kmhhwrhCpg5sxO7N82tOJ7hHxWv_u-Yw/edit?usp=sharing

I made a copy of the script on the Sheet Script

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The calendar id is declared on line 8

var calendarId = 'bora-bora.dk_is0cr9ibe4thrs4mkqghvudrrk@group.calendar.google.com';

It is called by two functions:
1 = syncFromCalendar on line 223. Synchronize from calendar to spreadsheet.

var calendar = CalendarApp.getCalendarById(calendarId);

2 = syncToCalendar on line 307. Synchronize from spreadsheet to calendar.

var calendar = CalendarApp.getCalendarById(calendarId);

The goal is to call the calendar ID programmatically to source the value from Cell B1 of the Data Sheet.
To do this, i) the variable should be removed, ii) add/insert new code to source the new value for the calendar ID, and iii) make adjustments to the functions that call for the value of the calendar ID.

Action:

1 - comment out Line 8
2 - Insert the following code at Line 9

// Get the calendar code from Sheet "Data", cell B1
function getcalendarId() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spreadsheet.getSheetByName("Data");
  var calrange = sheet.getRange("B1");
  var calid =calrange.getValue();
  return calid;
}

3 - Locate function = syncFromCalendar
3.1 - Comment out this line

var calendar = CalendarApp.getCalendarById(calendarId);

3.2 Insert/add these three lines above the commented line

// Get the calendar ID
var calidFrom = getcalendarId();
var calendar = CalendarApp.getCalendarById(calidFrom);

4 - Locate function = syncToCalendar
4.1 Comment out this line

var calendar = CalendarApp.getCalendarById(calendarId);

4.2 Insert/add these three lines above the commented line

// Get the calendar ID
var calidTo = getcalendarId();
var calendar = CalendarApp.getCalendarById(calidTo);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...