Skip to content

Instantly share code, notes, and snippets.

@bennettscience
Created October 21, 2016 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennettscience/2ad00a11ac6352004f62df89aa186fb8 to your computer and use it in GitHub Desktop.
Save bennettscience/2ad00a11ac6352004f62df89aa186fb8 to your computer and use it in GitHub Desktop.
Script to loop through a Google sheet and set returned data as a note on a selected cell.
// Globals yada yada
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var ui = SpreadsheetApp.getUi();
// Once all is said and done, add the info to the selected cell
function addNote(e) {
var cell = sheet.getActiveCell();
cell.setNote(e);
}
// Loop through the dates to find the ID match
function findDates() {
// The selected ID...
var idToMatch = sheet.getActiveCell().getValue();
// Get all the data from the lookup sheet
var range = ss.getSheetByName("raw").getDataRange();
var ids = range.getValues();
// Use the Logger for debugging. It'll make you feel better.
Logger.log(idToMatch);
// Initialize an empty array to hold returned dates from the loop.
var dates = [];
for(var i=0;i<ids.length;i++) {
// If the ID in the range matches your selected ID, push it into the array
if(ids[i][1] == idToMatch) {
var date = dates.push(" " + Utilities.formatDate(ids[i][0], "EST", "MM-dd-yy"));
}
}
// Send the array with dates to add the note to the cell
addNote(dates);
}
// Menus and interaction stuff
function onOpen() {
ui.createMenu("Get Dates")
.addItem("Run","findDates")
.addSeparator()
.addItem("Help","helpText")
.addToUi();
}
// Make the help text actually readable when you need help.
function helpText() {
var htmlOutput = HtmlService
.createHtmlOutput('<p style="font-family:sans-serif">Highlight a student\'s ID and select <b>Run</b> from the <b>Get Dates</b> menu above. The dates a student is logged will be added as a note to the cell.</p><br /><p style="font-family:sans-serif">Look at the dates by hovering over the cell with the note.</p>')
.setWidth(250)
.setHeight(300);
ui.showModalDialog(htmlOutput, 'Help');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment