Skip to content

Instantly share code, notes, and snippets.

@moisescastillo
Created September 30, 2021 17:54
Show Gist options
  • Save moisescastillo/869da3fc1abe9c3552bbfe2624011758 to your computer and use it in GitHub Desktop.
Save moisescastillo/869da3fc1abe9c3552bbfe2624011758 to your computer and use it in GitHub Desktop.
To-Do App Appscript GS
const ss = SpreadsheetApp.getActiveSpreadsheet();
function onOpen() {
let ui = SpreadsheetApp.getUi();
// Add custom menu
ui.createMenu('ToDo App')
.addItem('Open', 'showModal')
.addToUi();
}
function showModal() {
let guiForm = HtmlService.createTemplateFromFile("Gui").evaluate().setWidth(400).setHeight(800);
SpreadsheetApp.getUi().showModalDialog(guiForm, "📒 To-Do App");
}
function include(fileName) {
return HtmlService
.createHtmlOutputFromFile(fileName)
.getContent();
}
// Add row data
function addData(data) {
let sheet = ss.getSheetByName('tareas');
let fecha = new Date();
let mes = parseInt(fecha.getMonth()) + 1;
sheet.appendRow([
fecha.getTime().toString(),
data.tarea,
0
]);
}
// Get all data
function getAllData() {
let data = Array();
let sheet = ss.getSheetByName('tareas');
if ((sheet.getLastRow() - 1) > 0) {
data = sheet.getRange(2,1,sheet.getLastRow()-1,3).getValues();
}
return data;
}
// Get num row
function getNumRow(data, idCliente) {
let numRow = null;
let encontrado = false;
let i = 1;
data.forEach(row => {
if (row[0].toString() == idCliente) {
numRow = i;
encontrado = true;
}
i++;
});
numRow = (encontrado) ? (numRow + 1) : 0;
return numRow;
}
// Delete
function deleteRow(idCliente) {
let data = Array();
// Get num row
data = getAllData();
let numRow = getNumRow(data, idCliente);
// Delete row
if (numRow > 0) {
let sheet = ss.getSheetByName('tareas');
sheet.deleteRow(numRow);
}
}
// Update estatus
function cambiarEstatus(idCliente) {
let data = Array();
// Get num row
data = getAllData();
let numRow = getNumRow(data, idCliente);
// Update
if (numRow > 0) {
let sheet = ss.getSheetByName('tareas');
let estatus = sheet.getRange(numRow,3).getValue();
sheet.getRange(numRow,3).setValue((estatus == 1) ? 0 : 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment