admin管理员组文章数量:1356522
I basically want to get the last column of the first row of a spreadsheet. No matter what I try it Logs the last column with data of the whole spreadsheet. I know it's pretty simple, but no luck.
I have searched all over and everything I try doesn't work. Any help is greatly appreciated. Thanks so much! Brandon
I basically want to get the last column of the first row of a spreadsheet. No matter what I try it Logs the last column with data of the whole spreadsheet. I know it's pretty simple, but no luck.
I have searched all over and everything I try doesn't work. Any help is greatly appreciated. Thanks so much! Brandon
Share Improve this question asked Feb 27, 2015 at 15:07 BrandonMBrandonM 1531 gold badge4 silver badges12 bronze badges 1- 1 please post your code – Marc Commented Feb 27, 2015 at 16:00
5 Answers
Reset to default 2Here is some code to get you started:
function getLastNonEmptyCellInRow() {
var rowToCheck = 2;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var maxColumns = sheet.getLastRow();
var rowData = sheet.getRange(rowToCheck, 1, 1, maxColumns).getValues();
rowData = rowData[0]; //Get inner array of two dimensional array
var rowLength = rowData.length;
for (var i = 0; i < rowLength; i++) {
var thisCellContents = rowData[i];
Logger.log('thisCellContents: ' + thisCellContents);
if (thisCellContents === "") {
Logger.log(i);
return i + 1; //Pass the count plus one, which is the last column with data
}
}
}
function getLastColNum(rowNum)
{
// Get the values of the row's (unbounded by column number) range.
var vals =
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
.getRange(rowNum +":"+ rowNum)
.getValues()[0],
lastColNum = vals.length
;
// Decrement while popping off any trailing empty text ("") cells.
while(!vals.pop())
{ --lastColNum; }
return(lastColNum);
}
There's no built in function to do this, but you can write a function to do it yourself.
For example, use getRange() and getLastColumn() to get the row you are interested in up to the last possible data column, then use getValues() to get all the cells in that row. Finally, loop over array of cells (returned by getValues()) and keep track of the last index which has a value.
https://developers.google./apps-script/reference/spreadsheet/sheet#getRange()
https://developers.google./apps-script/reference/spreadsheet/range#getValues()
The Sheet class supports getLastColumn() to return the last column with content in it.
https://developers.google./apps-script/reference/spreadsheet/sheet#getLastColumn()
Range has a getLastColumn() function, but it is the last column of the range and not necessarily the last column with data in it.
https://developers.google./apps-script/reference/spreadsheet/range#getLastColumn()
To get the last column with content in it, iterate over the row from the last Sheet column back to a non-empty cell.
/**
* Get last column with content in row
* @param {Sheet} sheet
* @param {integer} row
* @return {integer} last column with content, 0 if none found
*/
function getLatColInRow(sheet, row = 1) {
let lastColumn = sheet.getLastColumn();
let values = sheet.getRange(row,1,1,lastColumn).getValues()[0];
while ((values[lastColumn-1] == "") && (lastColumn>0)) {
lastColumn --;
}
return lastColumn;
}
from excel workbook object get sheet using getSheetAt(sheetNumber); then using sheet object getRow(0); then using cellIterator() you can iterate till last column of row. Example using XSSFWorkBook XSSFWorkBook wb=new XSSFWorkBook(FileInputStream object);//object got by passing excel file path to FileInputStream Sheet sh=wb.getSheetAt(0) or wb.getSheet("sheet name"); Row row=sh.getRow(0); Iterator cit=row.cellIterator();Cell cell; while(cit.hasNext()) { cell=cit.next(); }
when you e out of the while loop you will have last cell object in 'cell' variable
本文标签: javascriptGoogle Apps Script getLastColumn() of a specific rowStack Overflow
版权声明:本文标题:javascript - Google Apps Script .getLastColumn(); of a specific row? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743973984a2570803.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论