LoginSignup
2
7

More than 5 years have passed since last update.

Google Spreadsheet の予定リストを Google Calendar に追加する。

Posted at

Google Calendar でポチポチと予定を追加するのは面倒ですね。特に "XX まであと N 週間" といった予定を手動で入力するのは苦痛です。その点 Google Spreadsheet を使えば、予定のタイトルと時間のリストを簡単に生成できるので、そのリストをインポートするところを GAS で自動化しました。

add_events.gs
/**
 * | Start      | End        |   | Calendar  | Title | Done |
 * |------------|------------|---|-----------|-------|------|
 * | 3/26 13:00 | 3/26 14:00 |   |           | Lunch |      |
 * |------------|------------|---|-----------|-------|------|
 * | 3/26       |            | v | Birthdays | Alice |      |
 */
var INDEX = {
  START: 0,
  END: 1,
  WHOLE_DAY: 2,
  TITLE: 3,
  CAL: 4,
  DONE: 5,
}

function registerEvents() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];

  // This represents ALL the data
  var range = sheet.getDataRange();
  var values = range.getValues();

  for (var i = 0; i < values.length; i++) {
    var row = values[i];
    // Find the calendar if the name is specified, otherwise use the default calnder.
    var cal = CalendarApp.getCalendarsByName(row[INDEX.CAL])[0] || CalendarApp.getDefaultCalendar();
    // Skip if the event is already registered.
    if (row[INDEX.DONE] != "") {
      continue;
    }

    var start = new Date(row[INDEX.START]);
    var title = row[INDEX.TITLE];
    if (row[INDEX.WHOLE_DAY] == "") {
      var end = new Date(row[INDEX.END]);
      cal.createEvent(title, start, end);
    } else {
      cal.createAllDayEvent(title, start);
    }

    // Mark as done.
    var done = range.getCell(i + 1, INDEX.DONE + 1);
    done.setValue("");
  }
}

走らせ方

  1. Create a new spreadsheet in Google Docs
  2. Add a list of events [1]
  3. Click [Tool] > [Script Editor]
  4. Copy-paste this script.
  5. Run the registerEvents function

[1] Table example.

Start End All Day Calendar Title Done
3/26 13:00 3/26 14:00 Lunch
3/26 v Birthdays Alice

2
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
7