【问题标题】:Automatically generate a calendar document using Google Drive and Google Apps Script使用 Google Drive 和 Google Apps 脚本自动生成日历文档
【发布时间】:2014-02-06 01:38:57
【问题描述】:

我有一个关于 Google 文档和 Google Apps 脚本的问题。

是否可以使用 Google Apps 脚本在具有以下文档功能的 Google Drive 中“自动”创建日历文档。

  • 传统日历表
  • 准确的月份和年份标题日期
  • 准确放置在正确的日期列中

See example

我知道 Google Apps 脚本可以自动创建文档和添加内容。我只是不确定它是否可以“自动”和“准确”地将日期放置在正确的位置。如果我寻求的目标不是一个选项,我不想开始这条路。

【问题讨论】:

  • 我很确定这是完全可行的,虽然并不容易......如果我有更多的空闲时间,我会很想尝试一下 :-)
  • “Google Drive 中的日历文档”是硬性要求吗?如果是这样,为什么?我认为使用 Google 日历会容易得多,因为这样您就可以利用日历服务 (developers.google.com/apps-script/reference/calendar)。 Google 日历可以与您的联系人共享,就像任何 Google Drive 文档一样...
  • @DanielBank,是的。我正在努力让其他老师在教学网站上使用。谷歌文档让他们“轻松”地嵌入他们的网站并使用链接(其他网站和文档)进行更新。我知道 Google 日历的大部分功能,我不“相信”您可以轻松地将链接添加到事件的 URL。
  • @Sergeinsas 谢谢,您认为将“正确”日期放入“正确”表格单元格的部分将成为“具有挑战性”的部分吗?

标签: google-apps-script google-drive-api google-docs


【解决方案1】:

我很快就把它拼凑起来了,但你应该可以很容易地修改它。您可以通过获取每月第一天的星期几,然后将日期插入二维数组的相应单元格,来获取显示在右侧列中的日期。然后将数组作为表格插入。

function create_cal() {
  var doc = DocumentApp.create("Calendar"),
      body = doc.getBody(),
      cells = [
        ['','','','','','',''],
        ['','','','','','',''],
        ['','','','','','',''],
        ['','','','','','',''],
        ['','','','','','',''],
        ['','','','','','','']
        ],
      now = new Date(),
      date = new Date(now.getFullYear(), now.getMonth(), 1),
      cur_month = now.getMonth(),
      row = 0,
      col = date.getDay();

  while (cur_month == date.getMonth()) {
    cells[row][col] = date.getDate();

    col += 1;
    if (col > 6) {
      col = 0;
      row += 1;
    }

    date = new Date(date.getTime() + 1000*60*60*24);
  }
  body.setText('Calendar Name');
  body.appendTable(cells);
}

【讨论】:

  • 谢谢@lagomorpho!我会在周末玩这个。
  • 这个在谷歌文档中生成了一个漂亮的日历。我正在寻找以这种方式在谷歌电子表格中生成日历的东西。我将为此使用这部分
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-05
  • 2015-07-10
相关资源
最近更新 更多