I need to have a sheet2 that always contains only the last 3 columns from sheet1.
Sheet1 starts out with columns having data in columns A,B,C,D,E,F,G, and then grows by one column each month.
So Sheet2 wont grow... Each Month the last three columns will overwrite
The only thing I can find is how to get the Last column
CodePudding user response:
I believe your goal is as follows.
- You want to retrieve the last 3 columns from the data range of a sheet.
- You want to achieve this using Google Apps Script.
In this case, how about the following sample script?
Sample script:
function myFunction() {
const sheetName = "Sheet1"; // Please set your sheet name.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const srcSheet = ss.getSheetByName(sheetName);
const values = srcSheet.getDataRange().getValues().map(r => r.splice(-3));
console.log(values); // You can confirm the retrieved values in the log.
}
When this script is run, the last 3 columns are retrieved from the data range of "Sheet1".
If you want to retrieve the last column instead of the last 3 columns, please modify
r.splice(-3)
tor.splice(-1)
.
References:
CodePudding user response:
Try this:
function myFunction() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("SheetName");
const lc = sh.getLastColumn();
const vs = sh.getRange(1,lc - 2,sh.getLastRow(),3).getValues();
console.log(vs);
}