Google Apps脚本:将时间表从电子表格传输到日历

本文适用于谁?



  • 对于积极使用Google日历服务的用户
  • 对于拥有带有时间表的表文件并且希望与他们更高效地合作的人们-总是在眼前有即将发生的事件,通过邮件或推送通知接收带有提醒的通知
  • 对于想快速了解Google Apps脚本的人,请了解它的含义以及可以在何处应用


需要什么



  • Google帐号
  • 基本的JavaScript知识


1.在Google表格中创建表格



这可以通过转到您的Google云端硬盘页面来完成。





数字:1.右键单击工作区,打开一个上下文菜单,您需要在其中选择“ Google表格”-“创建空表”



在创建的表格中,您需要复制自己的时间表,以我为例,这是大学的课程表。这些表必须包含以下字段,这一点很重要:



  • 活动标题
  • 活动日期
  • 开始时间
  • 结束时间或持续时间(但是,这不是必需的-因此,您可以为事件的持续时间设置一些常规默认值,例如1小时,甚至可以使事件持续一整天)


在我的情况下,所有其他信息-老师,课程类型(讲座,研讨会等)都可以发送到事件描述中。





. 2.



2.



2.1.



, , .





. 3. , "", " "



JavaScript. myFunction. , SetCalendar.





. 4. , — SetCalendar



, , . SetCalendar.



2.2.



JavaScript :



console.log("Hello, world!")


Google Apps Script . console Logger:



function SetCalendar() {
  Logger.log("Hello, world!");
}


, "", "" Ctrl+Enter:





. 5.



2.3.





function SetCalendar() {
  //         
  const rowStart = 1;
  const colStart = 1;

  //     
  const colsCount = 5;
  const rowsCount = 67;  

  //  
  var sheet = SpreadsheetApp.getActiveSheet();

  //     
  var range = sheet.getRange(rowStart, colStart, rowsCount, colsCount)
  var data = range.getDisplayValues();
}


11.



SpreadsheetApp — , , Google Sheets.



, — , ( ) . getActiveSheet.



sheet.



, range — , . getRange. :





! 1.

15: getDisplayValues.



! getDisplayValues() , . , , - , , .


2.4.



, , , . :



//-   
  const dateCol = 0;
  const timeCol = 1;
  const typeCol = 2;
  const nameCol = 3;
  const teacherCol = 4;


2.



data , . .



  for (var i in data)
  {
    let row = data[i];

    let classDate = row[dateCol];
    let classPeriod = row[timeCol];
    let classType = row[typeCol];
    let className = row[nameCol];
    let classTeacher = row[teacherCol];

    Logger.log("[DATE] " + classDate);
    Logger.log("[PERIOD] " + classPeriod);
    Logger.log("[TYPE] " + classType);
    Logger.log("[NAME] " + className);
    Logger.log("[TEACHER] " + classTeacher);
    Logger.log("============================================");
  }


for i data, i — , .



3: data i row — .



5-9: — . , row[dateCol]dateCol.



5-9 , , , .



, , .



2.5.



,



function SetCalendar() {
  //         
  const rowStart = 1;
  const colStart = 1;

  //     
  const colsCount = 5;
  const rowsCount = 8;  

  //  
  var sheet = SpreadsheetApp.getActiveSheet();

  //     
  var range = sheet.getRange(rowStart, colStart, rowsCount, colsCount)
  var data = range.getDisplayValues();

  //-   
  const dateCol = 0;
  const timeCol = 1;
  const typeCol = 2;
  const nameCol = 3;
  const teacherCol = 4;

  for (var i in data)
  {
    let row = data[i];

    let classDate = row[dateCol];
    let classPeriod = row[timeCol];
    let classType = row[typeCol];
    let className = row[nameCol];
    let classTeacher = row[teacherCol];

    Logger.log("[DATE] " + classDate);
    Logger.log("[PERIOD] " + classPeriod);
    Logger.log("[TYPE] " + classType);
    Logger.log("[NAME] " + className);
    Logger.log("[TEACHER] " + classTeacher);
    Logger.log("============================================");
  }
}


, :





. 6.



, , , , . ?



:





. 7.



, . , , .



, , , .



, :



  let savedDate = "";

  for (var i in data)
  {
    let row = data[i];

    let classDate = row[dateCol];

        //...

    if (classDate.trim() == "")
    {
      classDate = savedDate;
    }
    else
    {
      savedDate = classDate;
    }

    Logger.log("[DATE] " + classDate);
    //...
    Logger.log("============================================");
  }


savedDate — .



. — , savedDate, savedDate.



2.6.



getDisplayValues(), , , .



, Google Calendar, Date: - , - .



(. 2), dd.mm.yyyy, , , hh:mm-hh.mm.



, Date. Google Apps Script, - . , JS-, - - .



:



function extractTime(timeStr, dateStr)
{
  let sepIdx = timeStr.indexOf(":");

  let hoursStr = timeStr.substring(0, sepIdx);
  let minsStr = timeStr.substring(sepIdx + 1);

  sepIdx = dateStr.indexOf(".");

  let dayStr = dateStr.substring(0, sepIdx);
  let monthStr = dateStr.substring(sepIdx + 1, sepIdx + 3);

  sepIdx = dateStr.indexOf(".", sepIdx + 1);

  let yearStr = dateStr.substring(sepIdx + 1);

  let t = new Date();
  t.setHours(parseInt(hoursStr), parseInt(minsStr));
  t.setYear(parseInt(yearStr));
  t.setMonth(parseInt(monthStr) - 1, parseInt(dayStr));

  return t;
}

function extractPeriod(periodStr, dateStr)
{
  let sepIdx = periodStr.indexOf("-");

  let fromStr = periodStr.substring(0, sepIdx);
  let toStr = periodStr.substring(sepIdx + 1);

  fromStr = fromStr.trim();
  toStr = toStr.trim();

  return {
    from: extractTime(fromStr, dateStr),
    to: extractTime(toStr, dateStr)
  }
}


- , extractPeriod — , .



2.7. Google Calendar



,



let classTimeInfo = extractPeriod(classPeriod, classDate);

let classStartTime = classTimeInfo.from;
let classEndTime = classTimeInfo.to;

let info = ": " + classTeacher + "\n : " + classType;

var event = (CalendarApp.getCalendarsByName(""))[0].createEvent
(
  className,
  classStartTime,
  classEndTime,
  {
    description: info
  }
);

Utilities.sleep(50);


1-6: - , , .



, Google Calendar CalendarApp.



getCalendarsByName .



- getDefaultCalendar, . , - Google Calendar , . -, , .



, , , - , - , , , , , , , .



"", . getCalendarsByName "".



, , :



(CalendarApp.getCalendarsByName(""))[0]


- createEvent. :



  • ( )
  • Data — -
  • Data — -
  • — (description)


— 50 . , , , Google Calendar , API .



3.



,



! , 2. , , , .


function extractTime(timeStr, dateStr)
{
  let sepIdx = timeStr.indexOf(":");

  let hoursStr = timeStr.substring(0, sepIdx);
  let minsStr = timeStr.substring(sepIdx + 1);

  sepIdx = dateStr.indexOf(".");

  let dayStr = dateStr.substring(0, sepIdx);
  let monthStr = dateStr.substring(sepIdx + 1, sepIdx + 3);

  sepIdx = dateStr.indexOf(".", sepIdx + 1);

  let yearStr = dateStr.substring(sepIdx + 1);

  let t = new Date();
  t.setHours(parseInt(hoursStr), parseInt(minsStr));
  t.setYear(parseInt(yearStr));
  t.setMonth(parseInt(monthStr) - 1, parseInt(dayStr));

  return t;
}

function extractPeriod(periodStr, dateStr)
{
  let sepIdx = periodStr.indexOf("-");

  let fromStr = periodStr.substring(0, sepIdx);
  let toStr = periodStr.substring(sepIdx + 1);

  fromStr = fromStr.trim();
  toStr = toStr.trim();

  return {
    from: extractTime(fromStr, dateStr),
    to: extractTime(toStr, dateStr)
  }
}

function SetCalendar() {
  //         
  const rowStart = 1;
  const colStart = 1;

  //     
  const colsCount = 5;
  const rowsCount = 8;  

  //  
  var sheet = SpreadsheetApp.getActiveSheet();

  //     
  var range = sheet.getRange(rowStart, colStart, rowsCount, colsCount)
  var data = range.getDisplayValues();

  //-   
  const dateCol = 0;
  const timeCol = 1;
  const typeCol = 2;
  const nameCol = 3;
  const teacherCol = 4;

  let savedDate = "";

  for (var i in data)
  {
    let row = data[i];

    let classDate = row[dateCol];
    let classPeriod = row[timeCol];
    let classType = row[typeCol];
    let className = row[nameCol];
    let classTeacher = row[teacherCol];

    if (classDate.trim() == "")
    {
      classDate = savedDate;
    }
    else
    {
      savedDate = classDate;
    }

    let classTimeInfo = extractPeriod(classPeriod, classDate);

    let classStartTime = classTimeInfo.from;
    let classEndTime = classTimeInfo.to;

    let info = ": " + classTeacher + "\n : " + classType;

    var event = (CalendarApp.getCalendarsByName(""))[0].createEvent
    (
      className,
      classStartTime,
      classEndTime,
      {
        description: info
      }
    );

    Utilities.sleep(50);
  }
}


4.









  • — , , , ( ). , API Google Sheets Google Apps Script , ;
  • JavaScript, , - .


我愿意接受建设性的批评,如果您指出不足之处,并提出可以改进的地方和可以改变的地方,我将很高兴。



接下来要读什么






All Articles