I need to copy data from a selected range (Y5:Z198) to cell (Y206) but somehow I can only make it to appendRow and paste only on column A. Can someone help me, please?
function CopyData(CopyData) {
var ss = SpreadsheetApp.getActive();
var sh1 = ss.getSheetByName("CAPA");
var sh2 = ss.getSheetByName("CAPA");
var rg1 = sh1.getRange("Y5:Z198");
var vA = rg1.getValues();
for (var i = 0; i < vA.length; i++) {
if (vA[i][1]) {
sh2.appendRow(vA[i]);
}
}
}
Try this code to copy the data
function CopyData(CopyData) {
const dstRow = 206;
let ss = SpreadsheetApp.getActive(),
sheet = ss.getSheetByName('CAPA'),
srcRange = sheet.getRange('Y5:Z198'),
srcValues = srcRange.getValues(),
filtered = srcValues.filter(item => item[1]); // filter the data being copied
// Define the range to insert
// 'Y'+dstRow -> Y206
// filtered.length -> the number of rows in the filtered array of data
// ':Z'+(dstRow-1+filtered.length) -> bottom right cell
sheet.getRange('Y'+dstRow+':Z'+(dstRow-1+filtered.length)).setValues(filtered);
}
Related
Here is the description of my problem.
I have a range of placeholder text and its associated values in a spreadsheet. The placeholder text also exists in a slide presentation which gets replaced using the replacealltext function. However, the colors in the spreadsheet for the values do not change. Please see the examples below.
Google Apps Script:
// Google Sheets
var masterSheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = masterSheet.getSheetByName('Monthly Report'); //gets the data from the active pubfile
var range = sheet.getRange('S1:T110');
var data = range.getValues();
var titleName = sheet.getRange('AA14:AA14').getValues().toString();
data.shift(); //removes the headers in the sheets
// Creating the new slide
var spreadsheetID = SpreadsheetApp.getActiveSpreadsheet().getId(); //Ensure Code is applied to active Pubfile
var spreadsheetFolder = DriveApp.getFileById(spreadsheetID);
var parentFolder = spreadsheetFolder.getParents();
var folderID = parentFolder.next().getId();
var reportTemplate = DriveApp.getFileById('SLIDE ID'); // Gets the report Template
var copiedTemplate = reportTemplate.makeCopy(titleName, DriveApp.getFolderById(folderID)); //Makes a copy of the orginal and saves it in the specified folder
var newPresoID = copiedTemplate.getId();
var skeleton = SlidesApp.openById(newPresoID); //opens the new template presentation
var slides = skeleton.getSlides(); //calls the new slides
return newSlide1();
function newSlide1() {
var slide1new = slides[0]; // defining the location of the new slide 1
data.forEach(function (row) {
var templateVariable = row[0]; // First column contains variable names
var templateValue = row[1]; // Second column contains values
slide1new.replaceAllText(templateVariable, templateValue); //replaces all the text that matches the placeholders
slide1new.
});
As you can see, this code just replaces the value but does not bring in the source formatting.
Here are snapshots of the original spreadsheet data and the slides:
Source Data
Destination Slide with Placeholders
Current Result
Desired Result
Please suggest a way to fix this issue. Thank you!
Here is an example of applying the colors of a spreadsheet cell to the table cells of a slide.
My spreadsheet looks like this.
My slide looks like this.
Code.gs
function myFunction() {
try {
let spread = SpreadsheetApp.openById("xxxxxxxxxxxxxxxxxxxxxxxxxxx");
let sheet = spread.getSheetByName("Sheet1");
let range = sheet.getRange(2,1,sheet.getLastRow()-1,2);
let values = range.getDisplayValues();
let colors = range.getFontColors();
let present = SlidesApp.getActivePresentation();
let slide = present.getSlides()[0];
let table = slide.getTables()[0];
for( let i=0; i<table.getNumRows(); i++ ) {
let row = table.getRow(i);
for( let j=1; j<row.getNumCells(); j++ ) {
let text = row.getCell(j).getText();
values.some( (value,k) => {
let l = text.replaceAllText(value[0],value[1]);
if( l > 0 ) {
text.getTextStyle().setForegroundColor(colors[k][1]);
return true;
}
return false;
}
);
}
}
}
catch(err) {
console.log(err);
}
}
And the final results look like this.
Reference
TableCell
Array.some()
I have spreadsheet1 with all the details as in this image(Spreadsheet1), there are columns with startDate and endDate with some dates. Now i have a different spreadsheet2 like in this image (spreadsheet2) with header row of all the dates in the year (from 01/01/2021 to 31/12/2021). Now startDate and endDate from spreadsheet1 should match the header in spreadsheet2 and put the values of the column Type from spreadsheet1 to the respective cells in spreadsheet2 (like it is present in spreadsheet2 image for reference). Below is the code i'm working with but i'm not reaching my goal. Please help me i'm new to coding world. Thank you.
function myFunction() {
let ss = SpreadsheetApp.getActiveSpreadsheet();
let sheet = ss.getActiveSheet();
let last_row = sheet.getLastRow();
let data = sheet.getRange("A2:E"+last_row).getValues();
let start_date = [];
let end_date = [];
let dates_between = [];
let id = [];
let name = [];
let message = [];
let dd = SpreadsheetApp.openById('1z5WB1sACp1zvgfyXDbAmYxklSZOMIC8kNi_3Yci-PkM');
let dsheet = dd.getActiveSheet();
let dlast_row = dsheet.getLastRow();
let ddata = dsheet.getRange('C2:NC'+dlast_row).getValues();
let did = dsheet.getRange('A2:A'+last_row);
for(let i = 0; i<data.length;i++){
// let id = data[i][0];
id.push(data[i][0]);
name.push(data[i][1]);
start_date.push(data[i][2]);
end_date.push(data[i][3]);
message.push(data[i][4]);
dates_between.push(DATES_BETWEEN(start_date[i], end_date[i]));
}
did.setValue(id);
}
function DATES_BETWEEN(dateFrom, dateTo) {
var t = dateFrom.getTime(),
tMax = dateTo.getTime(),
values = [];
while (t <= tMax) {
values.push(new Date(t));
t += 24000 * 3600;
}
return values;
}
If you're able to put your 'types' on the row at start date and at end date you can fill the gap in-between with Array.fill() method.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill
All you need is to get the row as an array and to put it back on the sheet. You don't even need to calculate dates in the gap. You just fill all these empty elements between filled start and end cells.
Here is your row/array: ['','','x','','','x','','']
Start cell is array.indexOf('x')
End cell is array.lastIndexOf('x')
To fill the gap with 'x' strings use array.fill('x', start, end)
You will get: ['','','x','x','x','x','','']
Below is my solution that doesn't use Dates. If your dates have the same format on both sheets and if your destination sheet always has the dates of the rows of your source sheet you can consider them as strings, and use them as keys of an object (a map in my case):
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var src_sheet = ss.getSheetByName('Sheet1');
var dest_sheet = ss.getSheetByName('Sheet2');
// get dates from first row of destination sheet
var dates = dest_sheet.getDataRange().getDisplayValues()[0].slice(2);
// get rows (without first row) from source sheet
var rows = src_sheet.getDataRange().getDisplayValues().slice(1);
// loop through the rows and get the table
var table = rows.map(row => {
// get variables from the row
var [id, name, type, start, end] = row.slice(0, 5);
// create empty Map with dates-keys (date1:'', date2:'', ...)
var dates_map = new Map(dates.map(date => [date, '']));
// assign 'type' to key['start date'] and to key['end date']
dates_map.set(start, type).set(end, type);
// create array (row) from values of the Map
var row_array = Array.from(dates_map.values());
// fill empty elements of the array between first and last 'type'
row_array.fill(type, row_array.indexOf(type), row_array.lastIndexOf(type));
// return row
return [id, name, ...row_array];
});
// set the table on the destination sheet
dest_sheet.getRange(2, 1, table.length, table[0].length).setValues(table);
}
The same code without comments:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var src_sheet = ss.getSheetByName('Sheet1');
var dest_sheet = ss.getSheetByName('Sheet2');
var dates = dest_sheet.getDataRange().getDisplayValues()[0].slice(2);
var rows = src_sheet.getDataRange().getDisplayValues().slice(1);
var table = rows.map(row => {
var [id, name, type, start, end] = row;
var dates_map = new Map(dates.map(date => [date,'']));
dates_map.set(start, type).set(end, type);
var row_array = Array.from(dates_map.values());
row_array.fill(type, row_array.indexOf(type), row_array.lastIndexOf(type));
return [id, name, ...row_array];
});
dest_sheet.getRange(2, 1, table.length, table[0].length).setValues(table);
}
Just in case, this is a destructuring assignment:
var [id, name, type, start, end] = row;
It means:
var id = row[0];
var name = row[1];
var type = row[2];
var start = row[3];
var end = row[4];
Here is the link to my dummy spreadsheet.
I want to create a new worksheet each time I have a new user details in column 1 of my USERS sheet. Here is the code I have so far:
// Get the data from the sheet called CreateSheets
var sheetNames = SpreadsheetApp.getActive().getSheetByName("USERS").getDataRange().getValues();
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("USERS");
var data = ss.getDataRange().getValues();
var lr = ss.getLastRow();
var dataRange = ss.getRange(1, 1, lr, 1);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
// For each row in the sheet, insert a new sheet and rename it.
sheetNames.forEach(function(row) {
var sheetName = data;
var sheet = SpreadsheetApp.getActive().insertSheet();
sheet.setName(sheetName);
});
}}
The code works but it is combining the data in the cells in column 1 into the name of the new spreadsheet. Thanks
function myfunc() {
const ss = SpreadsheetApp.getActive();
const ush = ss.getSheetByName("USERS");
const names = ush.getRange(1, 1, ush.getLastRow(), 1).getValues().flat();
const enames = ss.getSheets().map(s => s.getName());
names.forEach(n => {
if (~e.names.indexOf(n)) {
ss.insertSheet().setName(n);
enames.push(n); //add name to array to avoid duplicate names in column
}
});
}
Updated to account for existing sheet names and potential duplicate names in column 1.
I think this should do what you want. You should probably build in some sort of check to ensure the sheet doesn't already exist.
function makeSheetHappen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var wsUser = ss.getSheetByName("USERS");//the sheet with users
//this gets all values in column 1 to end of spreadsheet (it might include blanks)
//flat function avoids having to pull 2-dim array (h/t COOPER!)
var sheetNames = wsUser.getRange(1, 1, wsUser.getLastRow(), 1).getValues().flat();
//mapping function to get an array of the spreadsheet's sheet names.
var theExistingNames = ss.getSheets().map(function (aSheet) {
return aSheet.getName();
});
//loops through the sheetNames and ensures not blank and not currently existing.
sheetNames.forEach(function (aName) {
if (aName != '' && !theExistingNames.includes(aName)) {
var newSheet = ss.insertSheet();
newSheet.setName(aName);
theExistingNames.push(aName); //add name to array to avoid duplicate names in column
}
});
}
I want to add the word "Flag" into column "G" or Array [6] where the corresponding row shows a value greater than 0.5 in column "E" or Array [5]. Note, that Array [6] is empty and only the script can add a value there if condition is met.
Here is my attempt but it does not add the word "Flag" into the cell.
I appreciate any help or pointer. Thanks in advance!
function test() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Sheet1');
var rg=sh.getDataRange()
var vA=rg.getValues();
var g = [];
for(var i=1;i<vA.length;i++) {
g[i] = [vA[i][6]];
if(Number(vA[i][5])>0.5)g[i] = ['Flag']; {
SpreadsheetApp.getActiveSheet().getRange(2,7,g.length,1).setValues(g);
}}}
The error states:Cannot convert Array to Object[].
Here is an amended version of your code that will work if all your data is plain values and not formulas.
Please note this will overwrite any formulas.
function test() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName('Sheet1');
var rg = sh.getDataRange();
var vA = rg.getValues();
for (var i = 1; i < vA.length; ++i) {
if (Number(vA[i][5]) > 0.5) {
vA[i][6] = 'Flag';
//change value of array element
}
}
rg.setValues(vA);
//set changed values to source range
}
Edit
This checks column F and makes changes to column G.
It will not overwrite the formulas in column F, but it will overwrite any formulas in column G.
function test() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName('Sheet1');
var lastRow = sh.getLastRow();
var checkRg = sh.getRange('F2:F' + lastRow);
var flagRg = sh.getRange('G2:G' + lastRow);
var checkVa = checkRg.getValues();
var flagVa = flagRg.getValues();
for (var i = 0; i < checkVa.length; ++i) {
if (Number(checkVa[i][0]) > 0.5) {
flagVa[i][0] = 'Flag';
//change value of array element
}
}
flagRg.setValues(flagVa);
//set changed values to source range
}
Not sure what is in columns A thru F and beyond G.
for(var i=1;i<vA.length;i++) {
if(Number(vA[i][5])>0.5) vA[i][6] = 'Flag';
}
SpreadsheetApp.getActiveSheet().getDataRange.setValues(vA);
Or if you only want to replace G.
var g = [];
for(var i=1;i<vA.length;i++) {
g[i-1] = [vA[i][6]]; // Notice its an array
if(Number(vA[i][5])>0.5) g[i-1] = ['Flag']; // Notice an array again
}
SpreadsheetApp.getActiveSheet().getRange(2,7,g.length,1).setValues(g);
I've just started using Google Apps script to manage some sheets for a project i'm working on, I am new to Javascript so please go easy if there are any howlers in my code!.
We have and app called forms2mobile that captures data and drops it into a Google spreadsheet. It actually drops different data into different sheets depending on which part of the app you use.
I've hacked together a script that pulls all data from one sheet (source), and drops only certain columns into a second sheet (destination). It then deletes all rows from the source, and any blank rows from the destination.
The problem I have is with deleting blank rows from the destination. Typically the destination will have empty rows at the bottom, and the code I have will only delete empty rows within the range that contains data. So i'm always left with empty rows at the bottom.
The destination sheet will then be used as a data source for forms2mobile, which of course isn't happy with empty rows.
I've found the class getMaxRows() but i'm not sure how to implement it. If anyone could make any suggestions that would be great.
Cheers
Paul
function NEW_copyColumnNumbers( ) {
var spreadsheet_source = SpreadsheetApp.openById('1a89ZIUcy-8168D1damCV3Q9Ix0arQn9jGS6pgp');
var spreadsheet_target = SpreadsheetApp.openById('1GQiLt9utSH_6CV__oJwmcLOkI4E9iNIRPWU7Xr');
var range_input = spreadsheet_source.getRange("A2:CC407");
var range_output = spreadsheet_target.getRange("A"+(spreadsheet_target.getLastRow()+1));
var keep_columns = [66,66,10,11,12,13,14,23,26,31,69,71,74,75,80];
copyColumnNumbers(range_input, range_output, keep_columns);
clearEmptyRows();
clearSourceData();
}
function copyColumnNumbers( range_input, range_output, columns_keep_num ) {
// Create an array of arrays containing the values in the input range.
var range_values = range_input.getValues();
// Loop through each inner array.
for ( var i = 0, row_count = range_values.length; i < row_count; i++ ) {
// Loop through the indices to keep and use these indices to
// select values from the inner array.
for ( j = 0, col_keep_count = columns_keep_num.length; j < col_keep_count; j++ ) {
// Capture the value to keep
var keep_val = range_values[i][columns_keep_num[j]];
// Write the value to the output using the offset method of the output range argument.
range_output.offset(i,j).setValue(keep_val);
}
}
}
function clearEmptyRows() {
var ss = SpreadsheetApp.openById('1GQiLt9utSH_6CV__oJwmcLOkI4E9iNIRPWU7Xr');
var s = ss.getActiveSheet();
var values = s.getDataRange().getValues();
nextLine: for( var i = values.length-1; i >=0; i-- ) {
for( var j = 0; j < values[i].length; j++ )
if( values[i][j] != "" )
continue nextLine;
s.deleteRow(i+1);
}
//I iterate it backwards on purpose, so I do not have to calculate the indexes after a removal
}
function clearSourceData() {
var ss = SpreadsheetApp.openById('1a89ZIUcy-8168D1damCV3Q9Ix0arQn9jGS6pgp');
var s = ss.getActiveSheet();
var data = s.getDataRange().getValues();
for(var n =data.length+1 ; n<0 ; n--){
if(data[n][0]!=''){n++;break}
}
s.deleteRows(2, (s.getLastRow()-1));
}
This is how it works :
function removeEmptyRows(){
var sh = SpreadsheetApp.getActiveSheet();
var maxRows = sh.getMaxRows();
var lastRow = sh.getLastRow();
sh.deleteRows(lastRow+1, maxRows-lastRow);
}
Note : you can handle columns the same way if necessary using getMaxColumn(), getLastColumn() and deleteColumns(number, howMany)
EDIT
by the way, here is also another way to delete empty rows in a spreadsheet... if you combine both it will "clean" your sheet entirely !
function deleteEmptyRows(){
var sh = SpreadsheetApp.getActiveSheet();
var data = sh.getDataRange().getValues();
var targetData = new Array();
for(n=0;n<data.length;++n){
if(data[n].join().replace(/,/g,'')!=''){ targetData.push(data[n])};
Logger.log(data[n].join().replace(/,/g,''))
}
sh.getDataRange().clear();
sh.getRange(1,1,targetData.length,targetData[0].length).setValues(targetData);
}
Demo sheet in view only - make a copy to use
Script to removeEmptyRows and removeEmptyColumns in Google Sheets. It puts together everything Serge and apptailor mentioned previously. Here is a sample sheet with the script included File > Make a copy... to edit a copy of the sheet. Also a video that shows you how to use this sheet.
//Remove All Empty Columns in the Entire Workbook
function removeEmptyColumns() {
var ss = SpreadsheetApp.getActive();
var allsheets = ss.getSheets();
for (var s in allsheets){
var sheet=allsheets[s]
var maxColumns = sheet.getMaxColumns();
var lastColumn = sheet.getLastColumn();
if (maxColumns-lastColumn != 0){
sheet.deleteColumns(lastColumn+1, maxColumns-lastColumn);
}
}
}
//Remove All Empty Rows in the Entire Workbook
function removeEmptyRows() {
var ss = SpreadsheetApp.getActive();
var allsheets = ss.getSheets();
for (var s in allsheets){
var sheet=allsheets[s]
var maxRows = sheet.getMaxRows();
var lastRow = sheet.getLastRow();
if (maxRows-lastRow != 0){
sheet.deleteRows(lastRow+1, maxRows-lastRow);
}
}
}
Just a quick note, I added this "if" statement to keep Serge insas's code from throwing an error if there is no empty bottom row when you are trying to remove empty rows.
Place this if around the last line function removeEmptyRows() and it
will not throw an error:
if (maxRows-lastRow != 0){
sh.deleteRows(lastRow+1, maxRows-lastRow);
}
Removing all empty lines (bottom-up)
before
after
function isEmptyRow(row){
for (var columnIndex = 0; columnIndex < row.length; columnIndex++){
var cell = row[columnIndex];
if (cell){
return false;
}
}
return true;
}
function removeEmptyLines(sheet){
var lastRowIndex = sheet.getLastRow();
var lastColumnIndex = sheet.getLastColumn();
var maxRowIndex = sheet.getMaxRows();
var range = sheet.getRange(1, 1, lastRowIndex, lastColumnIndex);
var data = range.getValues();
sheet.deleteRows(lastRowIndex+1, maxRowIndex-lastRowIndex);
for (var rowIndex = data.length - 1; rowIndex >= 0; rowIndex--){
var row = data[rowIndex];
if (isEmptyRow(row)){
sheet.deleteRow(rowIndex + 1);
}
}
}
function removeEmptyLinesFromAllSheets(){
SpreadsheetApp.getActive().getSheets().forEach(removeEmptyLines);
}
Removing only empty lines from below and above the data
before
after
function isEmptyRow(row){
for (var columnIndex = 0; columnIndex < row.length; columnIndex++){
var cell = row[columnIndex];
if (cell){
return false;
}
}
return true;
}
function getFirstNonBlankRowIndex(data){
for (var rowIndex = 0; rowIndex < data.length; rowIndex++){
var row = data[rowIndex];
if (!isEmptyRow(row)){
return rowIndex;
}
}
return 0;
}
function removePaddedEmptyLines(sheet){
var lastRowIndex = sheet.getLastRow();
var lastColumnIndex = sheet.getLastColumn();
var maxRowIndex = sheet.getMaxRows();
var range = sheet.getRange(1, 1, lastRowIndex, lastColumnIndex);
var data = range.getValues();
var firstRowIndex = getFirstNonBlankRowIndex(data);
sheet.deleteRows(lastRowIndex+1, maxRowIndex-lastRowIndex);
sheet.deleteRows(1, firstRowIndex);
}
function removePaddedEmptyLinesFromAllSheets(){
SpreadsheetApp.getActive().getSheets().forEach(removePaddedEmptyLines);
}
I have tried this piece of code and it works good, you may take a look and try it:
function DeleteBlankRows(){
var sh = SpreadsheetApp.getActiveSheet();
var maxRows = sh.getMaxRows();
var lastRow = sh.getLastRow();
for (var Raw = 1; Raw < sh.getLastRow() ; Raw++)
{
if( sh.getRange('A'+Raw).getValue() == '')
{
sh.deleteRow(Raw) //deleteRows(lastRow+1, maxRows-lastRow);
}
}
This works perfectly for me.
function removeEmptyRows(){
var spreadsheet = SpreadsheetApp.openById("IDOFYOURSPREADSHEETFOUNDINURL");
var sh = SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
var maxRows = sh.getMaxRows();
var lastRow = sh.getLastRow();
sh.deleteRows(lastRow+1, maxRows-lastRow);
}
This version allows you to specify top rows you don't want removed and also to ignore columns after ignoreAfterCol in case you don't want some columns considered when you are looking for blanks:
function removeEmptyLines(sheet,ignoreFirstRows,ignoreAfterCol){
sheet=ss.getSheetByName('Sheet12')
//get data and boundaries
var allData = sheet.getRange(1,1,sheet.getMaxRows(),ignoreAfterCol).getValues();
var sheetLength = allData.length;
while(allData[allData.length-1].toString().replace(/,/g,'')=='') allData.pop();
var lastPopulatedRow = allData.length;
//delete empty rows from end
var rowsToDeleteFromEnd = sheetLength - lastPopulatedRow;
if(rowsToDeleteFromEnd > 0) sheet.deleteRows(lastPopulatedRow+1,rowsToDeleteFromEnd);
//iterate through rows and delete blanks one by one
for(var i=lastPopulatedRow-1; i>ignoreFirstRows; i--){
if(allData[i].toString().replace(/,/g,'')=='') sheet.deleteRow(i+1);
}
}
this will help to delete exactly what you want:
Plus point:
you can check as many columns as you want to identify if a row is empty
this will also delete blank rows that contain formula
improve performance: this script deletes directly the empty rows according to their position without iteration through all the rows.
function deleteBlankRows(start_row=4) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
//temporarily insert last column to avoid affecting existing data
sheet.insertColumnsAfter(sheet.getMaxColumns(),1);
var lastRow = findLastRow();
var lastCol = sheet.getMaxColumns()
var temp_col = sheet.getRange(start_row,lastCol,lastRow-start_row,1)
//insert formula to show row position if any row is blank from column A to N (can adjust if needed)
sheet.getRange(start_row,lastCol).setFormula('=if(countif(A'+start_row+':N'+start_row+',"*?")=0,row(),0)').copyTo(temp_col)
//get a reversed list of rows position excluded non-empty rows
var rowsPosition = temp_col.getValues().filter(x => x != 0).reverse()
//delete empty rows from bottom to top
rowsPosition.forEach(function(rowPosition){
if (Number(rowPosition) > start_row) {
sheet.deleteRow(Number(rowPosition))
}
})
//finally, delete the temporary column
sheet.deleteColumn(lastCol)
}
function findLastRow() {
const sh = SpreadsheetApp.getActive().getActiveSheet();
const data = sh.getRange("A:L").getValues();
const mR = sh.getMaxRows();
const indexes = [];
data[0].forEach((_, ci) => {
let col = data.map(d => d[ci]);
let first_index = col.reverse().findIndex(r => r != '');
if (first_index != -1) {
let max_row = mR - first_index;
indexes.push(max_row);
}
});
last_row = indexes.length > 0 ? Math.max(...indexes) : 0;
return last_row;
}
function deleteblankRw(){
var sheet=SpreadsheetApp.getActive().getSheetByName('test')
var e=sheet.getRange('A'+sheet.getMaxRows()).getNextDataCell(SpreadsheetApp.Direction.UP).getRow()
for (k=2;k<=e;k++) {
if(sheet.getRange('A'+k).getValue()=='') {
sheet.deleteRow(k);
k=2;e--
if(k==e){break};
SpreadsheetApp.flush();
}
}
}