[英]Sharing a Calendar in Google App Script
我正在嘗試通過電子表格構建日歷,然后與域中的適當人員共享日歷。 我這樣做是作為附加到電子表格的腳本執行的。
到目前為止,我可以閱讀正確的單元格並使用事件構建日歷,但是我遇到的問題是弄清楚如何與正確的人共享特定的日歷。
我四處搜尋,發現相同的通用代碼可以手動請求網頁以允許訪問,但我嘗試使用這些訪問均無濟於事。 我是否錯過了其他所有人都假設您已完成的步驟? 我所做的就是在電子表格上編寫腳本,是否需要進行一些設置才能使其正常工作?
如果無法通過腳本編寫,那么解決這種問題的最佳方法是什么? 使用PHP和cron作業會更好嗎?
我共享日歷的代碼當前為:
function onEdit() {
calendarSharing('nic@xyz.com', 'boris@xyz.com');
};
function calendarSharing(user1,user2){
var scope = 'https://www.google.com/calendar/feeds/';
var fetchArgs = googleOAuth_('calenders', scope);
var rawXml= "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gAcl='http://schemas.google.com/acl/2007'>"+
"<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/acl/2007#accessRule'/>"+
"<gAcl:scope type='user' value='"+user2+"'></gAcl:scope>"+
"<gAcl:role value='http://schemas.google.com/gCal/2005#owner'> </gAcl:role></entry>"
fetchArgs.method = 'POST';
fetchArgs.payload = rawXml
fetchArgs.contentType = 'application/atom+xml';
try{
var url='https://www.google.com/calendar/feeds/'+user1+'/acl/full'
var urlFetch=UrlFetchApp.fetch(url,fetchArgs) //Giving Permission To personal account as a owner
}
catch(err){
var err1=err.toString()
var ex='Exception: Request failed for returned code 302'
if(ex==err1.split('.')[0]){
var tempCalenderUrl=[]
tempCalenderUrl.id = err1.split('<A HREF="')[1];
tempCalenderUrl.id = tempCalenderUrl.id.split('"')[0];
var url=tempCalenderUrl.id
try{
var urlFetch=UrlFetchApp.fetch(url,fetchArgs)
}
catch(err){}
}
}
}
function googleOAuth_(name,scope) {
var oAuthConfig = UrlFetchApp.addOAuthService(name);
oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oAuthConfig.setConsumerKey('anonymous');
oAuthConfig.setConsumerSecret('anonymous');
return {oAuthServiceName:name, oAuthUseToken:"always"};
}
您可以使用“ 高級日歷服務”讀取和寫入日歷共享信息。
該實用程序功能將在您具有writer
特權的任何日歷上插入或更新給定用戶的共享權限。 例如:
// Add user as reader
var rule = shareCalendar( 'gobbledygook@group.calendar.google.com',
'user@example.com');
此功能也可以作為要點的一部分使用。
/**
* Set up calendar sharing for a single user. Refer to
* https://developers.google.com/google-apps/calendar/v3/reference/acl/insert.
*
* @param {string} calId Calendar ID
* @param {string} user Email address to share with
* @param {string} role Optional permissions, default = "reader":
* "none, "freeBusyReader", "reader", "writer", "owner"
*
* @returns {aclResource} See https://developers.google.com/google-apps/calendar/v3/reference/acl#resource
*/
function shareCalendar( calId, user, role ) {
role = role || "reader";
var acl = null;
// Check whether there is already a rule for this user
try {
var acl = Calendar.Acl.get(calId, "user:"+user);
}
catch (e) {
// no existing acl record for this user - as expected. Carry on.
}
if (!acl) {
// No existing rule - insert one.
acl = {
"scope": {
"type": "user",
"value": user
},
"role": role
};
var newRule = Calendar.Acl.insert(acl, calId);
}
else {
// There was a rule for this user - update it.
acl.role = role;
newRule = Calendar.Acl.update(acl, calId, acl.id)
}
return newRule;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.