Skip to content

Instantly share code, notes, and snippets.

@j05u3
Created October 11, 2023 15:09
Show Gist options
  • Save j05u3/93f593b1f80baa388a1afef729fb6eac to your computer and use it in GitHub Desktop.
Save j05u3/93f593b1f80baa388a1afef729fb6eac to your computer and use it in GitHub Desktop.
Script to parse daily tasks log and export it to sheets
/**
* Ask permission only for this file
* @OnlyCurrentDoc
*/
function main() {
var daysWithTasks = calculateAllDaysTasks();
// store them in a spreadsheet
const spUrl = "https://docs.google.com/spreadsheets/d/1oNNrYG5qcaq6io38kpj2DegG4xPe0NLfESF8UzmSQZE/edit";
const sheetName = "tasks";
let fromRowIdx = 2;
// set header row
const headerRow = ["day", "startTime", "endTime", "duration", "description"];
const sp = SpreadsheetApp.openByUrl(spUrl);
const sheet = sp.getSheetByName(sheetName);
sheet.getRange(1, 1, 1, headerRow.length).setValues([headerRow]);
for (var _i = 0, daysWithTasks_1 = daysWithTasks; _i < daysWithTasks_1.length; _i++) {
var dayWithTasks = daysWithTasks_1[_i];
const day = dayWithTasks.day;
const tasks = dayWithTasks.tasks;
// append a row with the day for each task
for (var _j = 0, tasks_1 = tasks; _j < tasks_1.length; _j++) {
var task = tasks_1[_j];
const row = [day, task.startTime, task.endTime, task.duration, task.description];
sheet.getRange(fromRowIdx, 1, 1, row.length).setValues([row]);
fromRowIdx++;
}
}
console.log(new Date().toLocaleString());
// Logger.log('Showing durations for all days:');
// var s = "";
// for (var _i = 0, daysWithTasks_1 = daysWithTasks; _i < daysWithTasks_1.length; _i++) {
// var dayWithTasks = daysWithTasks_1[_i];
// Logger.log("");
// Logger.log("Day: ".concat(dayWithTasks.day));
// var totalDuration = dayWithTasks.tasks.reduce(function (acc, task) { return acc + task.duration; }, 0);
// Logger.log("Total duration: ".concat(totalDuration, " minutes"));
// var brTime = sumUpDurationsForRegex(dayWithTasks.tasks, /(break|comida)/i);
// Logger.log("Breaks/comida: ".concat(brTime, " minutes"));
// Logger.log("Not Breaks/comida: ".concat(totalDuration - brTime, " minutes"));
// var ucsur = sumUpDurationsForRegex(dayWithTasks.tasks, /(ucsur)/i);
// Logger.log("");
// Logger.log("ucsur: ".concat(ucsur, " minutes"));
// var bigbag = sumUpDurationsForRegex(dayWithTasks.tasks, /(bigbag)/i);
// Logger.log("bigbag: ".concat(bigbag, " minutes"));
// Logger.log("");
// s += [dayWithTasks.day, totalDuration - brTime, ucsur, bigbag].join(",") + "\n";
// }
// Logger.log(s);
}
// Compiled using undefined undefined (TypeScript 4.9.5)
function readAllDaysWithEntries() {
var _a;
var daysWithEntries = new Array();
// get all the HEADING2 titles
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var lastHeading2 = null;
var headings2 = body.getParagraphs().filter(function (p) { return p.getHeading() === DocumentApp.ParagraphHeading.HEADING2; });
for (var _i = 0, headings2_1 = headings2; _i < headings2_1.length; _i++) {
var p = headings2_1[_i];
Logger.log(p.getText());
if (lastHeading2 != null) {
var day = lastHeading2.getText();
// get all the vignette items in between the headings,
// includes sublists
var iterator = lastHeading2.getNextSibling();
var entries = new Array();
while (iterator != null &&
!(iterator.getType() === DocumentApp.ElementType.PARAGRAPH
&& iterator.asParagraph().getHeading() === DocumentApp.ParagraphHeading.HEADING2
&& iterator.asParagraph().getText() === p.getText())) {
Logger.log(iterator.getType());
if (iterator.getType() === DocumentApp.ElementType.LIST_ITEM) {
var text = iterator.asListItem().getText();
Logger.log(text);
// extract the time and the description
var _timeEntry = text.match(/^(\d{1,2}:\d{1,2})(.*)/);
if (_timeEntry != null) {
var time = _timeEntry[1];
var description = (_a = _timeEntry === null || _timeEntry === void 0 ? void 0 : _timeEntry[2]) !== null && _a !== void 0 ? _a : '';
Logger.log("time: ".concat(time, ", description: ").concat(description));
// parse time string to minutes
var _time = time.match(/(\d{1,2}):(\d{1,2})/);
var hours = parseInt(_time[1]);
var minutes = parseInt(_time[2]);
var totalMinutes = hours * 60 + minutes;
Logger.log("totalMinutes: ".concat(totalMinutes));
// add to the entries
entries.push({
time: totalMinutes,
description: description.trim()
});
}
}
iterator = iterator.getNextSibling();
}
daysWithEntries.push({
day: day,
entries: entries
});
}
lastHeading2 = p;
}
return daysWithEntries;
}
function calculateTasksFromTimeEntries(timeEntries, day) {
var tasks = new Array();
var lastEntry = null;
for (var _i = 0, timeEntries_1 = timeEntries; _i < timeEntries_1.length; _i++) {
var entry = timeEntries_1[_i];
if (lastEntry != null) {
var task = {
duration: entry.time - lastEntry.time,
startTime: lastEntry.time,
endTime: entry.time,
description: lastEntry.description
};
if (task.duration < 0) {
throw new Error("Task duration is negative: ".concat(task.duration, " for task ").concat(task.description, " on ").concat(day, " that starts at ").concat(task.startTime, " minutes"));
}
tasks.push(task);
}
lastEntry = entry;
}
return tasks;
}
function sumUpDurationsForRegex(tasks, exp) {
var totalDuration = 0;
for (var _i = 0, tasks_1 = tasks; _i < tasks_1.length; _i++) {
var task = tasks_1[_i];
if (exp.test(task.description)) {
totalDuration += task.duration;
}
}
return totalDuration;
}
function calculateAllDaysTasks() {
return readAllDaysWithEntries().map(function (dayWithEntries) {
var tasks = calculateTasksFromTimeEntries(dayWithEntries.entries, dayWithEntries.day);
return {
day: dayWithEntries.day,
tasks: tasks
};
});
}
function myFunction() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var rowsData = [['Plants', 'Animals'], ['Ficus', 'Goat'], ['Basil', 'Cat'], ['Moss', 'Frog']];
body.insertParagraph(0, doc.getName())
.setHeading(DocumentApp.ParagraphHeading.HEADING1);
var table = body.appendTable(rowsData);
table.getRow(0).editAsText().setBold(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment