我正在尝试通过电子表格构建日历,然后与域中的适当人员共享日历。我这样做是作为附加到电子表格的脚本执行的。
到目前为止,我可以阅读正确的单元格并使用事件构建日历,但是我遇到的问题是弄清楚如何与正确的人共享特定的日历。
我四处搜寻并找到了相同的常规代码来手动请求网页以允许访问,但我尝试使用这些访问均无济于事。我是否错过了其他所有人都假设您已完成的步骤?我所做的就是在电子表格上编写脚本,是否需要进行一些设置才能使其正常工作?
如果无法通过脚本编写,那么解决这种问题的最佳方法是什么?使用PHP和cron作业会更好吗?
我共享日历的代码当前为:
function onEdit() {
calendarSharing('[email protected]', '[email protected]');
};
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( '[email protected]',
'[email protected]');
此功能也可以作为要点的一部分使用。
/**
* 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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句