Skip to content

Instantly share code, notes, and snippets.

@danielo515
Created April 3, 2014 11:05
Show Gist options
  • Save danielo515/9952440 to your computer and use it in GitHub Desktop.
Save danielo515/9952440 to your computer and use it in GitHub Desktop.
function newRowManager(startCell,HeadersRow,sheetName)
{
/*---------USOS Válidos
newRowManager("A2");
newRowManager("A2",1");
newRowManager("A2",1,"Sheet1");
-----------
Devuelve un rowManager:
@Method getColumn
Devuelve objeto celda en la curRow que corresponde con la columName dada.
No distingue mayusculas de minúsuclas para el nombre de la columna.
@Method advanceRow avanza la curRow a la siguiente fila
*/
/* Setting defaults */
HeadersRow=HeadersRow || 1;//Si no se define la fila se toma la primera como la fila de las cabeceras
var sheet;
//Si no se da un de sheet se coje el sheet Activo por defecto
if(sheetName) sheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName); else sheet=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
/*Collecting data about the sheet*/
var lastRow=sheet.getLastRow();
var lastCol=sheet.getLastColumn();
var headersRange=sheet.getRange(HeadersRow,1,1,lastCol);
var startRange=sheet.getRange(startCell);
function getColumn(columnName)
{
var offset=this.headers.indexOf(columnName.toLowerCase());
if(offset >= 0)
return this.curRow.offset(0,offset);
}
function advanceRow()
{
var continuar=this.curRow.getRowIndex()<this.lastRow;
if ( continuar ) this.curRow=this.curRow.offset(1,0);
return continuar;
}
var rowManager={
headers:headersRange.getValues()[0].join('♦').toLowerCase().split('♦'),
curRow:startRange,
getColumn:getColumn,
lastRow:lastRow,
advanceRow:advanceRow
};
return rowManager;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment