1
votes

I have a question regarding Google Docs and Google Apps Script.

Is it possible to use Google Apps Script to 'automatically' create a calendar document in Google Drive with the following document features.

  • Traditional calendar table
  • Accurate month and year heading Dates
  • accurately placed in the correct day columns

See example

I know Google Apps Script can automatically create documents and add content. I'm just not sure it can "automatically" and "accurately" place dates in the correct locations. I don't want to start down the path if the goal I seek isn't an option.

1
I'm pretty sure it's perfectly doable although not really easy...If I had more free time I'd be tempted to try for fun :-)Serge insas
Is the "calendar document in Google Drive" a hard requirement? If so, why? I think it would be a lot easier to use a Google Calendar because then you can leverage the Calendar Service (developers.google.com/apps-script/reference/calendar). The Google Calendar could be shared with your contacts just like any Google Drive document...Daniel Bank
@DanielBank, yes. I'm working on this for other teachers to use on the teaching website. The google doc makes it 'easy' for them to embed in their site and update with links (other sites and documents). I'm aware of most of the capability of the Google Calendar and I don't 'believe' you can easily add links to URL's to events.Mr. B
@Sergeinsas Thanks, do you think the part about getting the 'correct' dates into the 'correct' table cells is going to be the "challenging" part?Mr. B

1 Answers

1
votes

I threw this together pretty quickly but you should be able to modify it pretty easily. You can get the dates to show up in the right columns by getting the day of the week of the first day of the month and then inserting dates into the corresponding cells of a 2D array. Then insert the array as a table.

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);
}