检查Google日历活动的存在 [英] Checking a Google Calendar Event's Existence

查看:64
本文介绍了检查Google日历活动的存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的整个代码中,我有一个非常基本的问题,这种问题已经持续了几天.我对此问题进行了广泛的研究,但是无法找到确切的解决方案(或者我错过了可能找到的解决方案).在这里:

I have a very basic problem within my whole code which became a head-ache for a couple of days. I have made an extensive research on this issue however couldn't be able to find an exact solution (or I have missed the one's that I might have found). Here it is:

我有一个电子表格,我在其中登录Google日历事件及其属性,包括事件ID.有时,我会手动清理Google日历,只是想运行一个代码来检查日历中是否存在事件,如果不删除,则删除该行.我的代码是:

I have a spreadsheet, where I log in Google Calendar events with their properties including the id of the event. Sometimes, I clean up the Google Calendar manually and just want to run a code which checks the events existence in the calendar and if not delete the row. My code is:

function cleanCal(calid, eventid) {
  var calid = 'my calendar id';
  var eventid = 'event id';
  var event = CalendarApp.getOwnedCalendarById(calid).getEventById(eventid);

  if (event) {
    event.deleteEvent();
    // clean-up sheet
  } else {
    // clean-up sheet
  }
}

基本上,如果事件在日历中,则代码应先将其删除,然后再清理工作表,如果事件不在日历中,则应仅清理工作表.但是,在执行代码时,尽管日历事件不存在,但是if语句返回true并在尝试删除该事件时引发错误,因为它实际上不存在.尽管事件不存在,但我无法找出事件对象返回 true 的原因,方式和原因.我在哪里做错了?感谢您的答复,我们非常感谢您的帮助.

Basically if the event is in the calendar, the code shall delete it first and then clean the sheet, if it's not there, it should clean the sheet only. However, when the code is executed, though the calendar event is not there, the if statement returns true and raises an error when trying to delete the event as it's actually not there. I haven't been able to find out the reason, how and why the event object return true though the event is not existing. Where am I doing wrong?? Thanks for replying and any help is really appreciated.

这是我用于通过Calendar API v3检查事件是否存在的代码

This is the code that I use to check events existence with Calendar API v3

function verifyCalendarEvent_OLD(calid, eventid) {
  var cal = CalendarApp.getCalendarById(calid)
  var exists = true;
  var response = Calendar.Events.list(
  calid, {
    showDeleted: true,
    fields: "items(id,status,summary)"
    }
  );

  for (var i = 0; i < response.items.length; i++) {
    if (response.items[i].id == eventid.split("@")[0] && response.items[i].status == "cancelled") {
      exists = false;
      break;
    }
  }
  return exists;
}

推荐答案

此答案如何?

例如,如果没有事件ID的事件,则CalendarApp.getOwnedCalendarById(calid).getEventById(eventid)返回null.当if对此进行评估时,将其用作false.

For example, if there is no event of the event ID, CalendarApp.getOwnedCalendarById(calid).getEventById(eventid) returns null. When this is evaluated by if, that is used as false.

因此,我认为您可能会尝试检索已删除的事件.因为在Google日历中,即使事件被删除,事件ID仍然保留.因此,尽管没有事件ID的事件,但是脚本中的if (event) {}返回true.我确认CalendarApp.getOwnedCalendarById(calid).getEventById(eventid)检索到已删除的事件.对于这种情况,您可以通过确认事件状态来知道是否删除了事件.

So I thought that you might try to retrieve the event which has already been removed. Because at Google Calendar, even if the event is removed, the event ID is remained. So although there is no event for the event ID, if (event) {} in your script returns true. I confirmed that CalendarApp.getOwnedCalendarById(calid).getEventById(eventid) retrieves the removed events. For this situation, you can know whether the event is removed by confirming the event status.

  • 事件状态为已确认时,表明该事件仍未删除.
  • 事件状态为已取消时,表明该事件已被删除.
  • When the event status is confirmed, it indicates that the event is still not removed.
  • When the event status is cancelled, it indicates that the event has already been removed.

使用此修改后的脚本时,请在高级Google服务和API控制台中启用Calendar API.

When you use this modified script, please enable Calendar API at Advanced Google Services and API console.

  • 在脚本编辑器上
    • 资源->高级Google服务
    • 打开Calendar API v3
    • On script editor
      • Resources -> Advanced Google Services
      • Turn on Calendar API v3
      • 在脚本编辑器上
        • 资源-> Cloud Platform项目
        • View API控制台
        • 在入门"中,单击启用API"并获取凭据(例如密钥).
        • 在左侧,单击库.
        • 在搜索API和服务,输入日历".然后点击Calendar API.
        • 单击启用"按钮.
        • 如果已启用API,请不要关闭.
        • 运行此脚本时,如果发生错误,则可能需要等待几分钟,直到启用API.
        • On script editor
          • Resources -> Cloud Platform project
          • View API console
          • At Getting started, click Enable APIs and get credentials like keys.
          • At left side, click Library.
          • At Search for APIs & services, input "Calendar". And click Calendar API.
          • Click Enable button.
          • If API has already been enabled, please don't turn off.
          • When you run this script, if an error occurs, you might be required to wait few minutes until the API is enabled.
          function cleanCal(calid, eventid) {
            var calid = 'my calendar id';
            var eventid = 'event id';
            var status = Calendar.Events.get(calid, eventid).status; // Added
          
            if (status == "confirmed") { // Modified
              var event = CalendarApp.getOwnedCalendarById(calid).getEventById(eventid); // Added
              event.deleteEvent();
              // clean-up sheet
            } else {
              // clean-up sheet
            }
          }
          

          如果我误解了你的问题,对不起.

          If I misunderstand your question, I'm sorry.

          在我的环境中,可以将手动和脚本删除的事件检索为status=cancelled.由于我不知道您的情况,因此我准备了一个示例脚本.此示例脚本是一个简单的流程.

          In my environment, the events removed by manual and script can be retrieved as status=cancelled. Since I didn't know your situation, I prepared a sample script. This sample script is a simple flow.

          1. 创建新事件.
          2. 删除创建的事件.
          3. 确认删除的事件.
            • 在这里,您使用了其他脚本.
          1. Create new event.
          2. Delete the created event.
          3. Confirm the deleted event.
            • Here, your additional script was used.

          示例脚本:

          function deleteeventa() {
            // Create new event
            var calid = 'my calendar id';
            var c = CalendarApp.getCalendarById(calid);
            var r = c.createEvent("sample event for deleting", new Date(2018,0,11,00,00), new Date(2018,0,11,01,00));
          
            // Delete the created new event
            var eventid = r.getId().split('@')[0];
            var event = c.getEventById(eventid);
            event.deleteEvent();
          
            // Your additional script
            var exists = true;
            var response = Calendar.Events.list(
            calid, {
              showDeleted: true,
              fields: "items(id,status,summary)"
              }
            );
            for (var i = 0; i < response.items.length; i++) {
              if (response.items[i].id == eventid && response.items[i].status == "cancelled") {
                exists = false;
                break;
              }
            }
            Logger.log("%s, %s, %s", r.getTitle(), r.getId(), exists)
          }
          

          结果:

          sample event for deleting, #####@google.com, false
          

          这篇关于检查Google日历活动的存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆