// Copyright 2016 Joseph W. May. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


/**
 * Base class that assists in working with a Sheet instance in a Spreadsheet.
 * 
 * @constructor
 * @param {string} sheetId The sheet id.
 */
var BaseSheet = function(sheetId) {
  this.config = Configuration.getCurrent();
  this.sheetId = sheetId;
  this.spreadsheet = new BaseSpreadsheet();
  this.sheet = this.spreadsheet.getSheetById(parseInt(sheetId));
  this.sheetName = this.sheet.getSheetName();
};


/**
 * Returns a Range instance representing a row in the sheet. An optional start
 * postion for the column can be providied. If no start column position or
 * number of columns are given, the entire row is returned, regardless of
 * content.
 * 
 * @param {number} rowNum The row to retrieve.
 * @param {number=} startCol An optional starting column postion.
 * @param {number=} numCols An optional number of columns.
 * @return {Range} A Range instance representing the row.
 */
BaseSheet.prototype.getRow = function(rowNum, startCol, numCols) {
  var row = this.getRows(rowNum, 1, startCol, numCols);
  return row;
};


/**
 * Returns a Range instance representing the given number of rows in the sheet.
 * An optional start postion for the column can be providied. If no start
 * column position or number of columns are given, the entire row is returned,
 * regardless of content.
 * 
 * @param {number} rowNum The row to retrieve.
 * @param {number} numRows The number of rows to retrieve.
 * @param {number=} startCol An optional starting column postion.
 * @param {number=} numCols An optional number of columns.
 * @return {Range} A Range instance representing the row.
 */
BaseSheet.prototype.getRows = function(rowNum, numRows, startCol, numCols) {
  var colStart = startCol === undefined ? 1 : startCol;
  var maxCols = this.sheet.getMaxColumns();
  numCols = numCols || (maxCols - colStart + 1);
  var row = this.sheet.getRange(rowNum, colStart, numRows, numCols);
  return row;
};


/**
 * Returns a Range instance representing a column in the sheet. An optional
 * start postion for the row can be providied. If no start position or number
 * of rows are given, the entire row is returned, regardless of content.
 * 
 * @param {number} colNum The column to retrieve.
 * @param {number=} startRow An optional starting row postion.
 * @param {number=} numRows An optional number of rows.
 * @return {Range} A Range instance representing the column.
 */
BaseSheet.prototype.getColumn = function(colNum, startRow, numRows) {
  var col = this.getColumns(colNum, 1, startRow, numRows);
  return col;
};


/**
 * Returns a Range instance representing the given number of columns in the
 * sheet. An optional start postion for the row can be providied. If no start
 * position or number of rows are given, the entire row is returned, regardless
 * of content.
 * 
 * @param {number} colNum The column to retrieve.
 * @param {number} numCols The number of columns to retrieve.
 * @param {number=} startRow An optional starting row postion.
 * @param {number=} numRows An optional number of rows.
 * @return {Range} A Range instance representing the column.
 */
BaseSheet.prototype.getColumns = function(colNum, numCols, startRow, numRows) {
  var rowStart = startRow === undefined ? 1 : startRow;
  var maxRows = this.sheet.getMaxRows();
  numRows = numRows || (maxRows - rowStart + 1);
  var col = this.sheet.getRange(rowStart, colNum, numRows, numCols);
  return col;
};


/**
 * Deletes the current sheet.
 */
BaseSheet.prototype.remove = function() {
  var ss = this.spreadsheet.getSpreadsheet();
  ss.deleteSheet(this.sheet);
};
@@ -3998,24 +3998,1048 @@
 n col;%0A%7D;%0A%0A%0A
+/**%0A * Returns the position of the last row that has content for the given column.%0A * %0A * @param %7Bnumber%7D colNum The column number.%0A * @param %7Bnumber=%7D startRow An optional starting row position.%0A * @param %7Bnumber=%7D numRows An optional number of rows.%0A * @return %7Bnumber%7D The last row of the column that contains content.%0A */%0ABaseSheet.prototype.getLastRow = function(colNum, startRow, numRows) %7B%0A  // Define range parameters.%0A  var rowStart = startRow === undefined ? 1 : startRow;%0A  var maxRows = this.sheet.getMaxRows();%0A  numRows = numRows %7C%7C (maxRows - rowStart + 1);%0A  var colValues = this.getColumn(colNum, rowStart, numRows).getValues();%0A  var colValuesLength = colValues.length;%0A  %0A  // Search from the end of the column to the top for the first row with%0A  // content. Empty elements are removed from the colValues array.%0A  for (var i = 0; i %3C colValuesLength; i++) %7B%0A    if (colValues%5BcolValuesLength - i - 1%5D%5B0%5D === '') %7B%0A      colValues.pop();%0A    %7D else %7B%0A      break;%0A    %7D%0A  %7D%0A  return colValues.length;%0A%7D;%0A%0A%0A
 /**%0A * Delet

add method to get last row with content