0

1つのGoogleシートから複数のGoogleカレンダーにイベントを作成するシートを作成しようとしています。私はMogsdadのこの記事Create Google Calendar Events from Spreadsheet but prevent duplicatesの素晴らしい解決策から修正されたシートを使用しています。しかし私は3つの異なるカレンダーに入るように私の仕事を三重にしてきており、私の最初のプログラミングに行きたいと思っています。私の考えは、さらに一歩進んで、(未確認、日付を保存し、確認した)ドロップダウン列(状態ラベル)を追加して、条件付きと同じ名前の1つまたは3つのカレンダー落ちる。複数のGoogleカレンダーでイベントをスプレッドシートから作成

私のシートは、以下のように配置されている: -

日|タイトル|スタート時刻|終了時刻|ロケーション|説明| ID |ステータス|確認された詳細|確認された開始時刻| |

私の考えは、確認されたカレンダーには他の2つの情報とは少し異なる情報が含まれていることがわかります。

私が使用している既存のコードは、だから私は、私はカレンダーだけでなく、2つの追加のカレンダーを「確認」に行くための新しい情報を定義する必要が実現

/** 
* Adds a custom menu to the active spreadsheet, containing a single menu item 
* for invoking the exportEvents() function. 
* The onOpen() function, when defined, is automatically invoked whenever the 
* spreadsheet is opened. 
* For more information on using the Spreadsheet API, see 
* https://developers.google.com/apps-script/service_spreadsheet 
*/ 
function onOpen() { 
    var sheet = SpreadsheetApp.getActiveSpreadsheet(); 
    var entries = [{ 
    name : "Export Events", 
    functionName : "exportEvents" 
    }]; 
    sheet.addMenu("Calendar Actions", entries); 
}; 

/** 
* Export events from spreadsheet to calendar 
*/ 
function exportEvents() { 
    var sheet = SpreadsheetApp.getActiveSheet(); 
    var headerRows = 1; // Number of rows of header info (to skip) 
    var range = sheet.getDataRange(); 
    var data = range.getValues(); 
    var calId = "[email protected]"; 
    var cal = CalendarApp.getCalendarById(calId); 
    for (i=0; i<data.length; i++) { 
    if (i < headerRows) continue; // Skip header row(s) 
    var row = data[i]; 
    var date = new Date(row[0]); // First column 
    var title = row[1];   // Second column 
    var tstart = new Date(row[2]); 
    tstart.setDate(date.getDate()); 
    tstart.setMonth(date.getMonth()); 
    tstart.setYear(date.getYear()); 
    var tstop = new Date(row[3]); 
    tstop.setDate(date.getDate()); 
    tstop.setMonth(date.getMonth()); 
    tstop.setYear(date.getYear()); 
    var loc = row[4]; 
    var desc = row[5]; 
    var id = row[6];    // Sixth column == eventId 
// Check if event already exists, delete it if it does 
try { 
    var event = cal.getEventSeriesById(id); 
    event.deleteEventSeries(); 
    row[6] = ''; // Remove event ID  
} 
catch (e) { 
    // do nothing - we just want to avoid the exception when event doesn't exist 
} 
//cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc}); 
var newEvent = cal.createEvent(title, tstart, tstop, {description:desc,location:loc}).getId(); 
row[6] = newEvent; // Update the data array with event ID 
debugger; 
    } 
    // Record all event IDs to spreadsheet 
    range.setValues(data); 
} 

です。私の問題は、3つのカレンダーにイベントを送るための一連のifループにどのように収まるか分かりません。カレンダーを追加したいと思います。 「未確認のカレンダー」に表示されるすべてのイベントは、そのステータスにアップレートされた日付を保存するために追加され、最後に「確定」に表示されます。したがって、確認されたイベントは3つのカレンダーすべてに表示されますが、確認されていないイベントはそこにのみ表示されます。

私はとてもいいことや他人の私の露骨な盗作が動作言い訳をしてください(感謝Mogsdad)プログラミングに新しい事実上のブランドだと私は任意の助けに感謝!

答えて

0

プログラミングへようこそ!ハングすると、使用しているすべてのGoogleサービスをスクリプト化する必要があります。私が正しくあなたの質問を理解して

場合:)、あなたが機能exportEvents()を実行すると、イベントがに入るどのカレンダーを選択できるようにしたいと思います。これを行うにはいくつかの方法があり、追加のループは必要ありません。オブジェクトを使用して名前で参照することができます。

私が最初だろう、あなたは現在calcalIdを定義し、このような3つのカレンダーを定義するオブジェクトを作成します:

var cal1 = "[email protected]"; 
var cal2 = "string url for second calendar"; 
var cal3 = "string url for third calendar"; 
var calendars = { 
    Unconfirmed: CalendarApp.getCalendarById(cal1), 
    SaveTheDate: CalendarApp.getCalendarById(cal2), 
    Confirmed: CalendarApp.getCalendarById(cal3) 
} 

オブジェクトcalendarsは今、このような3つのカレンダーのカレンダーのオブジェクトが含まれていますキーことをステータスとが対象です。あなたは各行のデータをつかんだときに、

var cal = row[7]; 

を追加し、calは状態を示す文字列が含まれています。あなたのnewEvent定義に1つの変更を行うことによって、連鎖の偉大な使用を行うことができます。

var newEvent = calendars[cal].createEvent(title, tstart, tstop, {description:desc,location:loc}).getId(); 

ここで何が起こっているcalendars[cal]あなたは、新しいイベントを追加することができたテーブル内の文字列に対応するカレンダーオブジェクトを取得しています。これはあなたのシートを変更する必要があります - ステータス列のラベルを 'Save the Date'から 'SaveTheDate'に変更して変数名に一致させます。それはそれを行う必要があります!複数のカレンダーにイベントを追加するには

EDIT

、私はifステートメントを使用しますが、あなたはループを必要としません。以下のようなものが動作します:

calendars['Unconfirmed'].createEvent(title... // Add to unconfirmed no matter what 
if (cal != 'Unconfirmed'){ 
    calendars['SaveTheDate'].createEvent(title... // Add to SaveTheDate only if not Unconfirmed 
} 
if (cal == 'Confirmed'){ 
    calendars['Confirmed'].createEvent(title... // Only add to Confirmed if Confirmed 
} 
+0

こんにちは、非常に役立ち、非常に役立ちます。私は質問したいフォローアップの質問があります。 3つのカレンダー、「SaveTheDate」、「SaveTheDate」、「Unconfirmed」カレンダー、「未確認」の各カレンダーに「確認済」イベントを表示し、「未確認」カレンダーにのみ移動します。 1つのカレンダーだけを選択するのではなく、どうやって追加することができるか知っていますか?私のオリジナルのアイデアでは、 "if"ループを持って各カレンダーに送っていましたが、あなたの方法は私に単純な方法があるように感じさせます!事前に感謝 –

+0

私の答えを参照してください - 私は助けるべき何かを追加しました。あなたが役に立つと思ったら私の答えを受け入れてください! –

+0

以前の回答が削除イベントの行が更新されるまで、重複を避けるために古いイベントを削除する機能を失ってしまう前に、私は忘れてしまった。 'try { var event =カレンダー['未確認'] .getEventSeriesById(id);'導入された2つの新しいカレンダーを除いて、すべて更新されたイベントを削除しません。私は、try { var event = calendars ['Confirmed']で2つのaddtional tryとcatch関数を追加しようとしました。getEventSeriesById(id); event.deleteEventSeries(); } catch(e){ } 'これはうまくいかないようです。実際に何も考えられていませんか? –

関連する問題