Skip to content

Instantly share code, notes, and snippets.

@rs77
Last active August 29, 2015 14:21
Show Gist options
  • Save rs77/589811c833ae7d121527 to your computer and use it in GitHub Desktop.
Save rs77/589811c833ae7d121527 to your computer and use it in GitHub Desktop.
if ( cell.length || cell ) // testing if a cell contains something other than an empty string or 0
// parsing through sheet - best practice, grab entire sheet and loop through array
data = sht.getDataRange().getValues();
for ( var r = 0; r < data.length; r += 1 ) { // rows are looped through first
for ( var c = 0; c < data[0].length; c += 1 ) { // columns are next
data[r][c];
}
}
// when pushing raw data back to the sheet (which doesn't contain any formulas - otherwise they will be overwritten):
sht.getRange( 1, 1, data.length, data[0].length ).setValues( data );
// from http://stackoverflow.com/a/1830844
function isNumber( n ) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
// http://stackoverflow.com/a/10589791
function isDate( d ) {
return d instanceof Date && !isNaN(d.valueOf());
}
// from http://stackoverflow.com/a/9436948/1618944
function isString( s ) {
return typeof s == 'string' || s instanceof String;
}
// if you have formulas it is best to have them behind frozen rows and columns. Then
// when you want to paste back in the raw data you can remove the frozen elements from
// the 2-d array and paste the data back in without interferring with the frozen formulas
function removeFrozens( data, fr, fc ) {
data.splice( 0, fr );
for ( var r = 0; r < data.length; r += 1 ) {
data[r].splice( 0, fc );
}
return data;
}
data = removeFrozens( data, fr, fc );
sht.getRange( fr + 1, fc + 1, data.length, data[0].length ).setValues( data );
// creating a menu
// https://developers.google.com/apps-script/articles/defining_menus
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [ {name: "Run a Function", functionName: "runThisFunction"},
null, // if you want a nice horizontal line
{name: "Do Something", functionName: "runThatFunction"} ];
ss.addMenu("ACC", menuEntries);
}
// delete everything except for the frozen ROWS
// handy for when you need to append new data into a sheet
data = sht.getDataRange().getValues();
fr = sht.getFrozenRows();
data.splice( fr );
// copying an array and passing it as a value to a function where you DON'T want the original
// array to be modified - http://stackoverflow.com/a/14491446/1618944
function( origArray ) {
var newArray = origArray.slice(0);
return newArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment