Transpose and Copy to another sheet using Apps script - google-apps-script

I need to transpose column data to rows based on the merged header using Apps Script.
Below is the view what would be my input and the expected output,
Input
Output
Sample sheet
What I've written so far:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange("A1:AO1");
var mergedValues = [];
//get the header added to the array
mergedValues.push(sheet.getRange("A2:I2").getValues());
Logger.log(mergedValues);
var mergedRanges = range.getMergedRanges();
for (var i = 0; i < mergedRanges.length; i++) {
var calcA1Notation = "A"+(i+3) + ":C"+(i+3);
var monA1Notation = "D"+(i+3) + ":F"+(i+3);
//Load the Transpose values into the array
mergedValues.push([[
sheet.getRange(calcA1Notation).getValues().toString(),
mergedRanges[i].getDisplayValue(),
sheet.getRange(monA1Notation).getValues().toString()
]]);
}
Logger.log(mergedValues[0].length);
for (var i = 0; i < mergedValues.length; i++){
//Writes to the lastrow+1 of the sheet
sheet.getRange(sheet.getLastRow()+1, 1).setValue(mergedValues[i]);
}
}
Can you guys help me in modifying google script to generate the expected result?

The question includes the term "Transpose", but this is misleading.
The goal of the questioner is straight-forward; to copy cells from one sheet to another. With one proviso, to include a column header from one sheet as a cell in the target range.
The questioner demonstrated code though they did not explain to what extent this was purposeful. The code takes three columns of data and concatenates the values into a single cell. At best, one might regard this as an early draft.
The referencing of the source data is uncomplicated; getting the month name is the main complication. I used two loops to work through the rows on the Source sheet because the questioner's intended outcome was that the data should sort by month.
I could have built a routine to convert the month string value to a numeric value, then sorted on that value (I certainly thought about it) - but I didn't;)
The Month names are in UPPERCASE, the questioner's outcome uses TitleCase. Again, I could have built a routine to convert the case, and I did spend some time trying. But in the end I decided that it was not a high priority.
function so5273586002() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Declare the two sheets
var sourcesheet = ss.getSheetByName("Input");
var targetsheet = ss.getSheetByName("Output");
// Get the respective starting row and ending rows.'' the target last row is declared in the loop.
var sourcestartrow = 3;
var targetstartrow = 2;
var sourcelastrow = sourcesheet.getLastRow();
// get the the data
var sourcerange = sourcesheet.getDataRange();
var sourcevalues = sourcerange.getValues();
// rubric for copying data.
// each row of the source must create two rows in the target - one row for each month
// the first three columns are repeats on both rows
// each row includes the source data as well as the month name
// target row #1
// source columns A, B & C to target A,B,C
// Month#1; value in D1 Source=> Target Column D (4)
// source columns DEF to target E F G
// target row #2
// source columns A, B & C to target A,B,C
// Month#2: value in G1 Source=> Target D (4)
// source fields G, H I to target E F G
// the questioner's prefered layout is that all the rows are sorted by month; to achive this, I used two loops
// the first to do the first month; the second to do the second month
for (i = sourcestartrow; i < (sourcelastrow + 1); i++) {
// get the last row for the target
var targetlastrow = targetsheet.getLastRow();
// Columns A, B and C -> Columns A, B and C
var targetRange = targetsheet.getRange(targetlastrow + 1, 1); //target: column =A, row = lastrow plus one
var sourcetest = sourcesheet.getRange(i, 1, 1, 3).copyTo(targetRange); // range = active row, column=A, 1 row, 3 columns, copy to SheetTracker
//Logger.log("source range is "+sourcesheet.getRange(i, 1, 1, 3).getA1Notation()+", target range is "+targetsheet.getRange(targetlastrow + 1, 1).getA1Notation());//DEBUG
// Month Name from the header
var targetRange = targetsheet.getRange(targetlastrow + 1, 4); //target: column =D, (month) row = lastrow plus one
var sourcetest = sourcesheet.getRange(1, 4).copyTo(targetRange, {
contentsOnly: true
}); // range = active row, column=A, 1 row, 3 columns, copy to SheetTracker
// Logger.log("source range is "+sourcesheet.getRange(1, 4).getA1Notation()+", target range is "+targetsheet.getRange(targetlastrow + 1, 4).getA1Notation());//DEBUG
// Month details
// Columns D E and F -> Columns E F and G
var targetRange = targetsheet.getRange(targetlastrow + 1, 5); //target: column =E, row = lastrow plus one
var sourcetest = sourcesheet.getRange(i, 4, 1, 3).copyTo(targetRange, {
contentsOnly: true
}); // range = active row, column=D(4), 1 row, 3 columns, copy to SheetTracker
// Logger.log("source range is "+sourcesheet.getRange(i, 4, 1, 3).getA1Notation()+", target range is "+targetsheet.getRange(targetlastrow + 1, 5).getA1Notation());//DEBUG
} // end loop#1
//Loop#2 to generate rows for the second month
for (i = sourcestartrow; i < (sourcelastrow + 1); i++) {
// get the last row for the target
var targetlastrow = targetsheet.getLastRow();
// Columns A, B and C -> Columns A, B and C
var targetRange = targetsheet.getRange(targetlastrow + 1, 1); //target: column =A, row = lastrow plus one
var sourcetest = sourcesheet.getRange(i, 1, 1, 3).copyTo(targetRange); // range = active row, column=A, 1 row, 3 columns, copy to SheetTracker
//Logger.log("source range is "+sourcesheet.getRange(i, 1, 1, 3).getA1Notation()+", target range is "+targetsheet.getRange(targetlastrow + 1, 1).getA1Notation());//DEBUG
// Month Name from the header
var targetRange = targetsheet.getRange(targetlastrow + 1, 4); //target: column =D, (month) row = lastrow plus one
var sourcetest = sourcesheet.getRange(1, 7).copyTo(targetRange, {
contentsOnly: true
}); // range = active row, column=G, 1 row, 3 columns, copy to SheetTracker
//Logger.log("source range is "+sourcesheet.getRange(1, 7).getA1Notation()+", target range is "+targetsheet.getRange(targetlastrow + 1, 4).getA1Notation());//DEBUG
// Month details
// Columns G H and I -> Columns E F and G
var targetRange = targetsheet.getRange(targetlastrow + 1, 5); //target: column =E, row = lastrow plus one
var sourcetest = sourcesheet.getRange(i, 7, 1, 3).copyTo(targetRange, {
contentsOnly: true
}); // range = active row, column=D(4), 1 row, 3 columns, copy to SheetTracker
// Logger.log("source range is "+sourcesheet.getRange(i, 7, 1, 3).getA1Notation()+", target range is "+targetsheet.getRange(targetlastrow + 1, 5).getA1Notation());//DEBUG
} // end loop#2
}
This screenshot shows the Source sheet ("Input").
These screenshots show the Target sheet ("Output") before and after running the code.
UPDATE
As noted in my comments, the earlier draft lacked two things:
1) it was inefficient and followed poor practices because it wrote the value of each field as it was created. The more appropriate approach would have been to write the data to an array, and then copy the array to the target range when the row-by-row processing was complete.
2) the code consisted of two loops to cater for the 2 months in the demonstration data. However, this is an impractical outcome since it is probable that there will be, in reality, any number of months' data in each row. Again, poor practice, when a more appropriate approach was to assume any number of month's data. The more efficient approach would have been to build an array of data while looping through each row.
This revision overcomes both drawbacks.
In addition, since month names do not sort in any meaningful sequence, I added a numeric month id that can be used for filtering and sorting in the output data sheet.
function so5273586003() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Declare the two sheets
var sourcesheet = ss.getSheetByName("Input");
var targetsheet = ss.getSheetByName("Output");
// Get the respective starting row and ending rows.'' the target last row is declared in the loop.
var targetstartrow = 2;
var sourcestartrow = 2;
var sourcelastrow = sourcesheet.getLastRow();
var sourcelastcolumn = sourcesheet.getLastColumn();
//Logger.log("the last row is "+sourcelastow+", and the last column is "+sourcelastcolumn);
// get the the data
var sourcerange = sourcesheet.getDataRange();
var sourcevalues = sourcerange.getValues();
var sourcelength = sourcevalues.length;
var i = 0;
var m = 0;
var month = 1;
var dataarray = [];
var masterarray = [];
// start loop by row
for (i = sourcestartrow; i < (sourcelastrow); i++) {
// start loop by month (within row)
for (m = 0; m <= (sourcelastcolumn - 6); m = m + 3) {
dataarray = [];
// add first three columns
dataarray.push(sourcevalues[i][0]);
dataarray.push(sourcevalues[i][1]);
dataarray.push(sourcevalues[i][2]);
//add the month name
dataarray.push(sourcevalues[0][3 + m]);
//add month data
dataarray.push(sourcevalues[i][3 + m]);
dataarray.push(sourcevalues[i][4 + m]);
dataarray.push(sourcevalues[i][5 + m]);
//create month id
switch (sourcevalues[0][3 + m]) {
case "JULY":
month = 1;
break;
case "AUGUST":
month = 2;
break;
case "SEPTEMBER":
month = 3;
break;
case "OCTOBER":
month = 4;
break;
case "NOVEMBER":
month = 5;
break;
case "DECEMBER":
month = 6;
break;
case "JANUARY":
month = 7;
break;
case "FEBRUARY":
month = 8;
break;
case "MARCH":
month = 9;
break;
case "APRIL":
month = 10;
break;
case "MAY":
month = 11;
break;
case "JUNE":
month = 12;
break;
default:
month = 100;
break;
} // end switch
// add the month id to the array (used for sorting)
dataarray.push(month);
// add the data to the master array before zeroing for next month
masterarray.push(dataarray);
} // months loop
} // end row loop
// get the length of the master array
var masterlength = masterarray.length;
// define the target range
var TargetRange = targetsheet.getRange(targetstartrow, 1, masterlength, 8);
// set the array values on the Target sheet
TargetRange.setValues(masterarray);
}

Related

Google Sheets Add row based on cell number value

I'm trying to make a google sheet script that adds a row based on cell value, basically if I have in the Quantity (Column D) 7x laptops, I want the script to add 6 additional rows below if Column H is marked as "Yes" through data validation.
What I was able to find and to do is only duplicate that row but is without data validation and I would prefer to add the data validation and possible make each quantity split to 1 (instead of 7) after the duplication.
`function autoDup() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getValues();
var newData = [];
for(var n in data){
newData.push(data[n]);
if(!Number(data[n][3])){continue};// if column 3 is not a number then do nothing
for(var c=1 ; c < Number(data[n][3]) ; c++){ // start from 1 instead of 0 because we have already 1 copy
newData.push(data[n]);//store values
}
}
sheet.getRange(1,1,newData.length,newData[0].length).setValues(newData).sort({column: 1, ascending: false});// write new data to sheet, overwriting old data
}`
Hope someone is able to help me.
Thank you,
Column D contains a qty and goods description. If Column H = "Yes", you want to insert a number of rows below Col D equal to the qty minus one. If Column H <> "Yes, then take no action.
Sample data - Before
Sample data - After
function so5925663201() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetname = "59256632";
var sheet = ss.getSheetByName(sheetname);
var row = 7;
// get value of Column H
var colHValue = sheet.getRange(row,8).getValue();
if (colHValue === "Yes"){
//Logger.log("DEBUG: Col H = yes. do something")
// get value of Column D
var Value = sheet.getRange(row,4).getValue();
var searchterm = "x";
var indexOfFirst = Value.indexOf(searchterm);
//Logger.log("DEBUG: the first instance of 'x' is "+indexOfFirst);
// get the quantity and convert from a string to a number
var qty = Value.substring(0, indexOfFirst);
var qtynum = +qty;
// var newtype = typeof qtynum; // DEBUG
//Logger.log("DEBUG: the quantity is "+qtynum+", new type = "+newtype)
// This inserts rows after
sheet.insertRowsAfter(row, qtynum-1);
}
else{
//Logger.log("DEBUG: col H <> Yes. do nothing");
}
}

Group rows using google apps scripts

I am writing code in which a user can automatically generate a template of lesson and sub-topics. Each lesson will have 10 sub-topics.
I also need to group the rows lesson-wise and topic-wise.
But, I am unable to group the rows lesson-wise and topic-wise. Tried using the macro-recorder, but the code does not work while generating multiple lessons.
EDIT: Working code is updated below.
function shiftrowgroupdepth() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
// start from row 6 and column 2
var row = 6;
var col = 2;
//Ask user for the no. of lessons
var shlen = Browser.inputBox("Enter no of lessons", Browser.Buttons.OK_CANCEL);
for (var i = 1; i <= shlen; i++) {
sheet.getRange(row,col).setValue("Lesson " + i);
row++;
Logger.log(spreadsheet.getCurrentCell().getRow())
sheet.getRange(row, 1, 70, sheet.getMaxColumns()).activate()
.shiftRowGroupDepth(1);
// Add sub-topics (1.1, 1.2 ....)
for (var j=1;j<=10;j++){
sheet.getRange(row,col).setValue(i+"."+j);
sheet.getRange(row+1, 1, 6, sheet.getMaxColumns()).activate()
.shiftRowGroupDepth(1);
row=row+7;
}
}
};
The OP code was very close to the mark. The main changes in this answer are:
When using a 'dot' separator for the topic codes, Google sheets treats the resulting value as a number; this creates problems displaying '1.10'. I changed the separator to a 'dash'. No doubt there is another potential approach using toString - but this was quick and easy.
The Lesson grouping is straightforward; 10 topics, 7 rows per topic = 70 rows.
Topic grouping had been complicated by referring to the location of the "current cell" - which could be anywhere on the sheet. I simplified this by using the row variable, which the OP had already (correctly) incremented.
function so5774532602() {
var ss = SpreadsheetApp.getActive();
var sheetname = "OPSheet";
var sheet = ss.getSheetByName(sheetname);
var row = 6;
var col = 2;
//Ask user for the no. of lessons
var shlen = Browser.inputBox("Enter no of lessons", Browser.Buttons
.OK_CANCEL);
for (var i = 1; i <= shlen; i++) {
sheet.getRange(row, col).setValue("Lesson " + i);
// add grouping
// Logger.log("DEBUG: i = "+i+", lesson range = "+sheet.getRange(+(row + 1), 2, 70, 1).getA1Notation());
sheet.getRange(+(row + 1), 2, 70, 1).activate()
.shiftRowGroupDepth(1);
row++;
// Add sub-topics (1.1, 1.2 ....) leave 6 blank rows below each sub-topic. Then, group those blank rows
for (var j = 1; j <= 10; j++) {
// Logger.log("DEBUG: i = "+i+", j = "+j+", row = "+row+", col = "+col); // new
sheet.getRange(row, col).setValue(i + "-" + j);
// add grouping
// Logger.log("DEBUG: range details: row = "+(row + 1) +",column = 1"+"number of rows = "+6+", number of columns = 1");
// Logger.log("DEBUG: topic range = "+sheet.getRange(+(row + 1), 2, 6, 1).getA1Notation());
sheet.getRange(+(row + 1), 2, 6, 1).activate()
.shiftRowGroupDepth(1);
row = row + 7;
}
}
}
Edit
Two minor changes for formatting
sheet.getRange(row,col).setValue("Lesson " + i).setHorizontalAlignment("center");
Centres the Lesson Number in the column.
sheet.getRange(row,col).setNumberFormat("#").setValue(i+"."+j).setHorizontalAlignment("center");
A return to a 'dot' separator but enables the tenth topic to display as 1.10, etc (credit #Tanaike). Will also center the text in the column.

Range selection and copying fixed value (from a cell) in that selection

Background
I have some code which is supposed to copy certain rows from Sheet B into Sheet A based on integer values in cells E1,J1 and I1. E1 has date format. After rows are copied from Sheet B to A, I need to fill column 12 (Column L) with the date from E1 to newly added rows.
https://docs.google.com/spreadsheets/d/15pTVfcoxM2wQTMC-3iLzXVXIEEaZFYXaOf97amy4yRg/edit?usp=sharing
Problem
The last three rows of code is not working well. Even though I am trying to select range for same column 12 (column L), it seems to select multiple columns and an additional 2 rows than what I had expected.
function test() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("B");
var aa = sheet.getRange("E1");
var Date = aa.getValue();
var aa = sheet.getRange("J1");
var lastrow = aa.getValue();
var aa = sheet.getRange("I1");
var lastrowV = aa.getValue();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("A");
var range = sheet.getRange(2, 1, lastrowV, 11);
var data = range.getValues();
sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("B");
sheet.getRange(lastrow, 1, data.length, 11).setValues(data); /* cell J1 gets updated after this*/
var aa = sheet.getRange("J1");
var lastrowN = aa.getValue() - 1;
range = sheet.getRange(lastrow, 12, lastrowN, 12);
range.activate();
sheet.getRange(lastrow, 12, lastrowN, 12).setValues(Date);
}
Background
The OP is attempting to insert a given date in the cell at the end of a row. However, the OP's definition of the range is faulty because it is selecting multiple columns (when only one column is required) and the number of rows is greater (by 2 (two)) than the number required. In addition, regardless of the range height, the OP is attempting to set a single value (rather than an array) into the range.
Problems
1) The definition of the datecolumn (Column L) included a value for the number of columns (probably a carry over from having defined the data range earlier).
Old range: getRange(lastrow,12, lastrowN, 12);. Delete the last parameter (number of columns) and the code behaves.
2) The code used this method setValues(Date) to populate the date column (8 rows in the OP's example data). the problem here is that the value assigned is the single value Date. not an array. This was addressed by creating and populating a temporary array datearray, and using this to update values in the date column.
3) In addition to the problems noted, the OP code is problematic in that a number of variables names were re-used with entirely different contexts (including "sheet" and "aa"), and some variables were declared multiple times. This made the code hard to read and debug. I took the opportunity to resolve as many of these as possible.
function so5473808801() {
// setup spreadsheet and sheets
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetB = ss.getSheetByName("B");
var sheetA = ss.getSheetByName("A");
// define key variables
// date
var daterange = sheetB.getRange("E1");
var datevalue = daterange.getValue();
// rows on SheetA
var Arows = sheetB.getRange("I1"); // = 9
var Alastrow = Arows.getValue();
// rows on sheet B
var Brows = sheetB.getRange("J1"); // = 3
var Blastrow = Brows.getValue();
// define the data range on Sheet A
var Adatarange = sheetA.getRange(2, 1, Alastrow, 11);
// Logger.log("DEBUG: The defined range on Sheet A is "+Adatarange.getA1Notation());//DEBUG
var Adatavals = Adatarange.getValues();
// define a target range on Sheet B and set values from A
var targetrange = sheetB.getRange(Blastrow, 1, Adatavals.length, 11);
// Logger.log("DEBUG: The target on sheetB = "+targetrange.getA1Notation()); // DEBUG
targetrange.setValues(Adatavals);
// set a range to update date on Sheet B
var daterows = (Alastrow - 1); // doesn't take 2 row header on B intoi account
var Bdaterange = sheetB.getRange(Blastrow, 12, daterows);
// Logger.log("DEBUG: The date range on sheet B = "+Bdaterange.getA1Notation());
// create an array to store multiple copies of datevalue
var datearray = [];
//populate the array
for (var i = 0; i < daterows; i++) {
datearray.push([datevalue]);
}
// set the date into Column L
Bdaterange.setValues(datearray);
}

Sending email when a date count down is triggered

I am trying to set up a sheet with varying email address and
dates. when a date (due) counts down to 20 say, I'd like to send the
owner of the issue an automatic email. All info in in the same row
just varying columns. I post my semi-functional script here, I can get the correct line item to pull based on date, I just cant get the script to pull the associated email with the date. Or I get a ton of emails that i dont want. Sheet
Any help would be much appreciated!!
function checkReminder() {
// get the spreadsheet object
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// set the first sheet as active
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
// fetch this sheet
var sheet = spreadsheet.getActiveSheet();
// Number of rows to process
var numRows = sheet.getLastRow()-1;
// figure out what the last row is
var lastRow = sheet.getLastRow();
// the rows are indexed starting at 1, and the first row
// is the headers, so start with row 2
var startRow = 2;
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 2 ,lastRow,
sheet.getLastColumn());
// Fetch values for each row in the Range.
var data = dataRange.getValues();
//Logger.log(data)
// grab column 20 (the 'days left' column) changed numrow to last r
row
get last row change
var range = sheet.getRange(startRow,20,lastRow-startRow+1,1 );
var numRows = range.getNumRows();
var days_left_values = range.getValues();
// Now, grab the reminder name column
range = sheet.getRange(2, 16, lastRow-startRow+1, 1);
var reminder_name_values = range.getValues();
var warning_count = 0;
var msg = "";
//msg = msg + "Trial Reminder Trial: "+reminder_name+" is due in
"+days_left+" days.\n"
//for (i in data) {
// var row = data[i];
// First column
// var emailAddress = row[13];
// Recipe column (Priority HIGH)
// var message = row[14];
// var subject = "Reminder CAPA ";
//subject = subject + reminder_name;
// Loop over the days left values
for (var k = 0; k <= numRows-1; k++) {
var days_left = days_left_values[k][0];
if(days_left == 20) {
// if it's exactly 20, do something with the data.
var reminder_name = reminder_name_values[k][0];
msg = msg + "Reminder CAPA: "+reminder_name+" is due in
"+days_left+" days.\n";
warning_count++;
}
if(warning_count) {
//MailApp.sendEmail(emailAddress, subject, message);
Logger.log(msg);
}
}}

Find LastRow of column C (when Col A and B have a different row size)?

How to find the last used cell of column C ?
Example: "Sheet1" : "Col A" and "Col B" have 1200 rows. And "Col C" has only 1 row.
## ColA ColB ColC
## 1 1 1
## 2 2 empty
## .. .. ..
## 1200 1200 empty
Here are my unsuccessful tests :
Function find_last_row_other_column() {
var ws_sheet =
var ws = SpreadsheetApp.openById("Dy...spreadsheet_id...4I")
var ws_sheet = ws1.getSheetByName("Sheet1");
var lastRow = ws_sheet.getRange("C").getLastRow();
var lastRow = ws_sheet.getRange("C:C").getLastRow();
var lastRow = ws_sheet.getRange(1,3,ws_sheet.getLastRow()); 1200 rows for colA! instead of row = 1 for col C.
}
Note: I can't use C1 because next time I use the function it will be C1200 or something else.
var lastRow = ws_sheet.getRange("C1").getLastRow();
I ask this because my next goal is to copy/paste the result of C1 into C2:C1200. Here is my test :
var lastRow = ws_sheet.getLastRow();
var target_range = ws_sheet.getRange(1,3,lastRow,1); //C1 until last row
var Formula_values = source_range.getValues();
target_range.setValues(Formula_values);
Thanks in advance ;)
ps: I have spend 2 hours on it. I have tried similar problems & their solutions already given on this website, but I can't happen to make them working. I am lost ! :
More efficient way too look up the last row in a specific column?
and Get last row of specific column function - best solution
As I mentioned in the comments above, this is the subject of the highest score post on StackOverFlow...
The original post returns the value of the last cell in a column but a (very) little modification makes it return the row index.
Original post :
Script:
function lastValue(column) {
var lastRow = SpreadsheetApp.getActiveSheet().getMaxRows();
var values = SpreadsheetApp.getActiveSheet().getRange(column + "1:" + column + lastRow).getValues();
for (; values[lastRow - 1] == "" && lastRow > 0; lastRow--) {}
return values[lastRow - 1];
}
modified to return index of the last used cell in a column :
function lastValue(column) {
var lastRow = SpreadsheetApp.getActiveSheet().getMaxRows();
var values = SpreadsheetApp.getActiveSheet().getRange(column + "1:" + column + lastRow).getValues();
for (; values[lastRow - 1] == "" && lastRow > 0; lastRow--) {}
return lastRow;
}
Here is the function to do it:
function lastRowInColumnLetter(column) {
var lastRow = SpreadsheetApp.getActiveSheet().getLastRow() - 1; // values[] array index
var values = SpreadsheetApp.getActiveSheet().getRange(column + "1:" + column + (lastRow + 1)).getValues();
while (lastRow > -1 && values[lastRow] == "") {
lastRow--;
}
if (lastRow == -1) {
return "Empty Column";
} else {
return lastRow + 1;
}
}
and you invoke it as =lastRowInColumnLetter("C").
And here are 3 more useful functions in this context:
function lastValueInColumnLetter(column) {
var lastRow = SpreadsheetApp.getActiveSheet().getLastRow() - 1; // values[] array index
var values = SpreadsheetApp.getActiveSheet().getRange(column + "1:" + column + (lastRow + 1)).getValues();
while (lastRow > -1 && values[lastRow] == "") {
lastRow--;
}
if (lastRow == -1) {
return "Empty Column";
} else {
return values[lastRow];
}
}
function lastValueInColumnNumber(column) {
var lastRow = SpreadsheetApp.getActiveSheet().getLastRow() - 1; // values[] array index
var values = SpreadsheetApp.getActiveSheet().getRange(1,column,lastRow + 1).getValues();
while (lastRow > -1 && values[lastRow] == "") {
lastRow--;
}
if (lastRow == -1) {
return "Empty Column";
} else {
return values[lastRow];
}
}
function lastRowInColumnNumber(column) {
var lastRow = SpreadsheetApp.getActiveSheet().getLastRow() - 1; // values[] array index
var values = SpreadsheetApp.getActiveSheet().getRange(1,column,lastRow + 1).getValues();
while (lastRow > -1 && values[lastRow] == "") {
lastRow--;
}
if (lastRow == -1) {
return "Empty Column";
} else {
return lastRow + 1;
}
}
These functions properly address empty columns, and also start counting backwards from the last row with content on the active sheet getLastRow(), and not from the last row on the sheet (with or without content) getMaxRows() as in the accepted answer.
If you don't have empty cells between your data, you can use this:
function last_Column_Row(){
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var Direction = SpreadsheetApp.Direction;
var xcol = 2;//e.g. for column 2 ("B"), to obtain its last row
var yrow = 8;//e.g. for row 8, to obtain its last column
var lastRow =sheet.getRange(1,xcol).getNextDataCell(Direction.DOWN).getRow();//last row of column 'xcol'
var lastCol =sheet.getRange(yrow,1).getNextDataCell(Direction.NEXT).getColumn();//last column of row 'yrow'
};
It gets the number of next empty cell-1 of a specific row or column (similar to Ctrl + 'arrow' in a sheet)
But If you have empty cells between your data, you can use this:
function last_Row_Column2()
{
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var Direction = SpreadsheetApp.Direction;
var maxR =sheet.getMaxRows();
var maxC = sheet.getMaxColumns();
var yrow = 8;//e.g. for row 8, to obtain its last column
var xcol = 2;//e.g. for column 2 ('B'), to obtain its last row
var valMaxR = sheet.getRange(maxR,xcol).getValue();//for the case that the last row has the last value
var valMaxC = sheet.getRange(yrow,maxC).getValue();//for the case that the last column has the last value
if(valMaxR !=''){var lastRow = maxR;}//if the last row in studied column is the last row of sheet
else{var lastRow =sheet.getRange(maxR,xcol).getNextDataCell(Direction.UP).getRow();}
if(valMaxC !=''){var lastCol = maxC;}//if the last column in studied row is the last column of sheet(e.g.'Z')
else{var lastCol =sheet.getRange(yrow,maxC).getNextDataCell(Direction.PREVIOUS).getColumn();}
};
[UPADTE} Please disregard this answer. User Serge's code instead. I was having a brain fart. His answer is magnitudes better in every way. That will teach me not to answer SO questions after you come back from a cocktail night... [/UPDATE]
The following function will log the last non-blank row number of column C. Note: if, for example, column C has a value in row 1 and row 200, with rows 2-199 blank, the function will return 200 as last non-blank row - it does not account for blank rows above last non-blank row.
function getLastNonBlankColCrow() {
var sheet = SpreadsheetApp.getActiveSheet();
var lastNonBlankColCrow = 0;
for (var i=1, lenRows=sheet.getRange("C:C").getNumRows(); i<=lenRows; i++) {
if ( !sheet.getRange(i, 3).isBlank() ) { // 3 is 1-based index of column C
lastNonBlankColCrow = i;
}
}
Logger.log(lastNonBlankColCrow);
}