Created
June 12, 2017 11:04
-
-
Save sathlan/67572a12b8da9394d9189b98ecbd94af to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NUMBER_OF_LINES = 51 | |
Date.prototype.addDays = function(days) { | |
var dat = new Date(this.valueOf()); | |
dat.setDate(dat.getDate() + days); | |
return dat; | |
} | |
function getNextValidDateFrom(addDay) { | |
var nextValid = new Date().addDays(addDay); | |
var day = nextValid.getDay(); | |
// Meet on Mon, Wed, Thu. | |
switch(day) { | |
case 1: case 3: case 4: | |
break; | |
case 2: case 7: | |
nextValid = nextValid.addDays(1); | |
break; | |
case 6: | |
nextValid = nextValid.addDays(2); | |
break; | |
case 5: | |
nextValid = nextValid.addDays(3); | |
} | |
return nextValid.toDateString(); | |
} | |
function insertDateAndTable(date, line) { | |
var doc = DocumentApp.getActiveDocument(); | |
var body = doc.getBody(); | |
var cursor = doc.getCursor(); | |
// var doc = DocumentApp.openById('11JTxIrSo0EeLF8QaFX2eACBAmwDFEYkmnakQgI3fOws').getBody(); | |
if (cursor) { | |
var element = cursor.getElement(); | |
var idx = element.getParent().getChildIndex(element) + 1; | |
var heading = body.insertParagraph(idx, 'Agenda for ' + date + "\n"); | |
heading.setHeading(DocumentApp.ParagraphHeading.HEADING3); | |
var table = body.insertTable(idx+1); | |
var tr = table.appendTableRow(); | |
var number_str = "0\n" | |
for(var j=1; j<line; j++){ | |
number_str += j + "\n"; | |
} | |
var td = tr.appendTableCell(number_str); | |
var td = tr.appendTableCell(''); | |
table.setColumnWidth(0,30); | |
table.setBorderWidth(0.5); | |
table.setBorderColor('#d3d3d3'); | |
} else { | |
DocumentApp.getUi().alert('Cannot find a cursor.'); | |
} | |
} | |
function createTodayAgenda() { | |
var date = getNextValidDateFrom(0); | |
insertDateAndTable(date, NUMBER_OF_LINES) | |
} | |
function createTomorrowAgenda() { | |
var date = getNextValidDateFrom(1); | |
insertDateAndTable(date, NUMBER_OF_LINES) | |
} | |
// Useless, keep it as example. | |
function cleanupTable() { | |
var tables = DocumentApp.getActiveDocument() | |
.getBody() | |
.getTables(); | |
var table, numRows, row, numCells, cell, bgCol; | |
for (var i = 0; i < tables.length; i++) { | |
table = tables[i]; | |
numRows = table.getNumRows(); | |
for (var j = numRows-1; j >= 0; j--) { | |
row = table.getRow(j); | |
numCells = row.getNumCells(); | |
if(numCells != 2) { | |
break; | |
} else { | |
DocumentApp.getUi().alert('AAA: ' + j + ":" + numRows); | |
} | |
cell = row.getCell(1).getText(); | |
if (cell == '') { | |
table.removeRow(j); | |
} else { | |
DocumentApp.getUi().alert('bbb: ' + j+ ":" + numRows); | |
} | |
} | |
} | |
} | |
function onOpen() { | |
var ui = DocumentApp.getUi(); | |
// Or DocumentApp or FormApp. | |
ui.createMenu('Scrum') | |
.addItem('Create today agenda', 'createTodayAgenda') | |
.addItem('Create tomorrow agenda', 'createTomorrowAgenda') | |
.addSeparator() | |
// .addItem('cleanup Tables', 'cleanupTable') | |
.addToUi(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment