2016-11-24 60 views
1

對不起我的英語不好...... :)希望你能理解我。如何使用javascript刪除輔助Google日曆

我正在與谷歌日曆的項目工作,直到現在我能夠創建日曆插入/刪除/修改事件,但我找不到一種方法來刪除日曆的'calendarID'。我使用javascript

這是由谷歌開發的「Java」的示例腳本,但將無法工作:

// Initialize Calendar service with valid OAuth credentials 
Calendar service = new Calendar.Builder(httpTransport, jsonFactory, credentials) 
    .setApplicationName("applicationName").build(); 

// Delete a calendar 
service.calendars().delete("secondaryCalendarId").execute(); 

這是我工作的代碼:

<script src="js/jquery.js" type="text/javascript"></script> 
    <script src="js/bootstrap.min.js"></script> 
    <script type="text/javascript"> 


       $(document).ready(function(){ 


       $(".btnCalCreate").hide(); 

       $("#btnCalCreate").click(function(){ 
       createCalendar();    

       }); 



       }); 



     // date variables 
     var now = new Date(); 
     today = now.toISOString(); 

     var twoHoursLater = new Date(now.getTime() + (2 * 1000 * 60 * 60)); 
     twoHoursLater = twoHoursLater.toISOString(); 

     // Google api console clientID and apiKey 
     var clientId = 'xxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com'; 
     var apiKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; 


     var scopes = 'https://www.googleapis.com/auth/calendar'; 

     // OAuth2 functions 
     function handleClientLoad() { 
      gapi.client.setApiKey(apiKey); 
      window.setTimeout(checkAuth, 1); 
     } 

     function checkAuth() { 
      gapi.auth.authorize({ client_id: clientId, scope: scopes, immediate: true }, handleAuthResult); 
     } 

     // show/hide the 'authorize' button, depending on application state 
     function handleAuthResult(authResult) { 
      var authorizeButton = document.getElementById('authorize-button'); 
      var eventButton  = document.getElementById('btnCreateEvents'); 
      var eventDelButton = document.getElementById('btnDeleteEvents'); 
      var eventGetList = document.getElementById('btnGetCalendarlist'); 
      var eventCreateCal = document.getElementById('btnCalCreate'); 
      var resultPanel  = document.getElementById('result-panel'); 
      var resultTitle  = document.getElementById('result-title'); 



      if (authResult && !authResult.error) { 
       $(".panel-login").text('Login in corso...'); 
       authorizeButton.style.visibility = 'hidden';  // if authorized, hide button 
       resultPanel.className = resultPanel.className.replace(/(?:^|\s)panel-danger(?!\S)/g, '')// remove red class 
       resultPanel.className += ' panel-success';   // add green class 
       resultTitle.innerHTML = 'Accesso Eseguito';  // display 'authorized' text 

       //eventButton.style.visibility = 'visible'; 
       //eventDelButton.style.visibility = 'visible'; 
       //eventGetList.style.visibility = 'visible'; 
       eventCreateCal.style.visibility = 'visible'; 
       $('#btnCalCreate').insertBefore('#authorize-button'); 
       $("#txtEventDetails").attr("visibility", "visible"); 
      } else {             // otherwise, show button 
       authorizeButton.style.visibility = 'visible';    
       $("#txtEventDetails").attr("visibility", "hidden"); 

       //eventButton.style.visibility = 'hidden'; 
       //eventDelButton.style.visibility = 'hidden'; 
       //eventGetList.style.visibility = 'hidden'; 
       eventCreateCal.style.visibility = 'hidden'; 

       resultPanel.className += ' panel-danger';   // make panel red 
       authorizeButton.onclick = handleAuthClick;   // setup function to handle button click 
      } 
      getAvailableCalendars(); 
     } 

     // function triggered when user authorizes app 
     function handleAuthClick(event) { 
      gapi.auth.authorize({ client_id: clientId, scope: scopes, immediate: false }, handleAuthResult); 
      return false; 
     } 

     function refreshICalendarframe() { 
      var iframe = document.getElementById('divifm') 
      iframe.innerHTML = iframe.innerHTML; 
     } 
     // setup event details 

     // function load the calendar api and make the api call 
     function makeApiCall() { 
      var eventResponse = document.getElementById('event-response'); 

      gapi.client.load('calendar', 'v3', function() {     // load the calendar api (version 3) 
       var request = gapi.client.calendar.events.insert 
       ({ 
        'calendarId': '[email protected]', // calendar ID 
        "resource": resource       // pass event details with api call 
       }); 

       // handle the response from our api call 
       request.execute(function (resp) { 
        if (resp.status == 'confirmed') { 
      eventResponse.innerHTML = "Event created successfully. View it <a href='" + resp.htmlLink + "'>online here</a>."; 
         eventResponse.className += ' panel-success'; 
         eventResponse.className += ' panel-LIST'; 
         refreshICalendarframe(); 
        } else { 
        document.getElementById('event-response').innerHTML = "There was a problem. Reload page and try again."; 
         eventResponse.className += ' panel-danger'; 
        } 
       }); 
      }); 
     } 
     var resource = { 
      "summary": "MyEvent", 
      "start": { 
       "dateTime": today 
      }, 
      "end": { 
       "dateTime": twoHoursLater 
      }, 
      "description":"We are organizing events", 
      "location":"US", 
      "attendees":[ 
      { 
        "email":"[email protected]", 
        "displayName":"Shaveta", 
        "organizer":true, 
        "self":false, 
        "resource":false, 
        "optional":false, 
        "responseStatus":"needsAction", 
        "comment":"This is event first", 
        "additionalGuests":3 

      }, 
      { 
       "email":"[email protected]", 
        "displayName":"Chatak", 
        "organizer":true, 
        "self":false, 
        "resource":false, 
        "optional":false, 
        "responseStatus":"needsAction", 
        "comment":"This is office event", 
        "additionalGuests":3 
      } 
      ], 
     }; 

     // FUNCTION TO DELETE EVENT 
     function deleteEvent() { 
     gapi.client.load('calendar', 'v3', function() { 
      var request = gapi.client.calendar.events.delete({ 
      'calendarId': '[email protected]', 
      'eventId': '8xxxxxxxxxj0vh50d5urouo74' 
      }); 
     request.execute(function(resp) { 
      alert(resp.status); 
      if (resp.status == 'confirmed') { 
       alert("Event was successfully removed from the calendar!"); 
      } 
      else{ 
       alert('An error occurred, please try again later.'); 
      } 
     }); 
     }); 
     } 

     // FUNCTION DELETE CALENDAR 

     function deleteCalendar(){ 

      var key  = $('#input-key').val();\\ this is calendars id 

      alert(key); 

      service.calendars().delete(key).execute(); 

      } 

// FUNCTION create calendar   

     function createCalendar() { 

     var name  = $('#input-name').val(); 

     for (var i = 0; i < aID.length; i++) { 

      if (aID[i] === name) { 
       $("#input-name").after('<div class=\"text-danger\">Nome calendario gi&agrave; presente!</div>'); 
       setTimeout(function(){$('.text-danger').hide();}, 800*10); 
       var name=''; 
       die; 
      } 
     } 


      //die; // remeber to remove 

     // check if leght of calendar name is good 

     if ((name <1) || (name > 50)) { 



      $("#input-name").after('<div class=\"text-danger\">Nome calendario non valido!</div>'); 
      setTimeout(function(){$('.text-danger').hide();}, 800*10); 

      return false; 
     } 




     var req = gapi.client.calendar.calendars.insert(
    { 
     "resource": 
     {"summary": name, 
     "description": "mycalendar", 
     "timezone": "Europe/Rome"} 
    }); 


    //$('.container').fadeOut(300); 
    //$('.gapiRequest').fadeIn(400); 
    $(".panel-login").text('Sending API request, please wait...'); 
    $("#btnCalCreate").prop("disabled", true); 

    req.execute(function(resp) { 



     calendarId=resp.id; 

     //$('.gapiRequest').fadeOut(200); 
     $(".panel-login").text('Accesso Eseguito'); 
     $("#btnCalCreate").prop("disabled", false); 
     //$('.container').fadeIn(300); 

     $('#input-key').val(calendarId); 
     //alert(calendarId); 

    }); 
     } 



     var aID=[]; 
// FUNCTION get list of calendars 
     function getAvailableCalendars() { 
     gapi.client.load('calendar', 'v3', function() { 
     var request = gapi.client.calendar.calendarList.list(); 
     request.execute(function(resp) { 
      var divCalendars = document.createElement('div'); 
      divCalendars.innerHTML = '<b>CALENDARI DISPONIBILI:</b><br />' 

      for (var i = 0; i < resp.items.length; i++) { 
      var calendarID = resp.items[i].id; 
      var calendarSummary = resp.items[i].summary; 
      var a = document.createElement('a'); 

      aID.push(calendarSummary); 
      //a.title = calendarSummary; 
      //a.innerHTML += a.title + /*' -> ' + calendarID + */'<br />'; 
      //a.href='http://www.sardiniacultureholidays.it/intakeCalInsert.cfm?calendar=' +calendarID; 
      //divCalendars.appendChild(a); 
      //document.getElementById('calendars').appendChild(divCalendars); 





      /**/ 

      } 
     }); 
     }); 
    } 

     </script> 

    <script src="https://apis.google.com/js/client.js?onload=handleClientLoad" type="text/javascript"></script> 

(對不起,代碼有點搞砸了)

經過很長時間搜索文檔和示例找不到方法來刪除日曆,希望你能幫我解決這個問題。

感謝您的幫助。

喜肯迪,所以我可以用這樣的事情?:

var IDCal = 'xxxxxxxxxxxxxxxxxxxxx' 

function(request, response) { 
     $.ajax({ 
       url: 'https://www.googleapis.com/calendar/v3/calendars/&IDCal=' + <?php echo $IDCal; ?>, 
      dataType: 'json', 
      success: function(json) { 
       response(resp) 
       })); 
      } 
     }); 
+0

用[日曆:刪除](https://developers.google.com/google-apps/calendar/v3/reference/calendars/delete),刪除一個次級日曆。以下是HTTP請求:「DELETE https:// www.googleapis.com/calendar/v3/calendars/calendarId」 有關詳細信息,請查看此[Google Calendar API with JavaScript](http:// www。 codeproject.com/Articles/986631/Google-Calendar-API-with-JavaScript)教程,瞭解如何創建,編輯和刪除事件和日曆。 – KENdi

+0

您好,感謝您的回覆,我知道您建議的鏈接。 BU目前尚不清楚怎麼用這個: DELETE https://www.googleapis.com/calendar/v3/calendars/calendarId 這樣的事情?: ' 功能(請求,響應){ \t \t $就({ \t \t \t網址:?「https://www.googleapis.com/calendar/v3/calendars/calendarId&IDcal= encodeURIComponent方法(請求), \t \t \t dataType:'json', \t \t \t成功:函數(JSON){ \t \t \t \t響應(RESP){ \t \t \t \t \t返回{ \t \t \t \t \t \t更迭:「?????' \t \t \t \t \t} \t \t \t \t})); \t \t \t} \t \t});' – Max

+0

對不起我的意見是與該微降價格式化有點搞砸了......我遇到的麻煩。 請參閱我的問題。 – Max

回答

1

那麼,這麼多的嘗試後finaly我發現刪除由ID日曆的方式。

這是函數:

function deleteCalendar(){ 

     var key = $('#input-key').val(); 

     gapi.client.load('calendar', 'v3', function() { 
     var request = gapi.client.calendar.calendars.delete({ 
     'calendarId': key 
     }); 

    request.execute(function(resp) {    
     if (typeof resp.code === 'undefined') { 
      alert("Calendar was successfully removed!"); 
     }else{ 
      alert('An error occurred... Error code: '+ resp.code +' - Message: '+resp.message); 
     } 

     }); 

    }); 

}