Move data to bottom of sheet - google-apps-script

I've got a list of dates, clients, and shift data in a spreadsheet (rows A5:D34). I want to write this data to the last row of the sheet. Unfortunately, the number of rows is variable. On one week, the data may be 17 rows; later, it may be 23 rows.
I want to "archive" the data by moving the used rows to the bottom of the sheet.
I'm looking for a code (google script) solution that can work with the fact that the range to be copied is always 4 columns wide, but a variable number of rows.
Thanks for all your help!

This is a quite common problem where we might not know or have to manually include the total number of rows in the sheet. We can use, getRange() with last row values or getDataRange() to extract the range.
getRange(start_row,start_column,number_of_rows,number_of_columns);
to use the last row in the above syntax starting from A1, simply change the parameters as below.
var range = getRange(1,1,sheet.getLastRow(),sheet.getLastColumn());
The second option is to use getDataRange() which is
var range = sheet.getDataRange();
PS : Mind that range is a 2D array.
Hope this helps :)

I would suggest using sheet.getDataRange() to know how many rows and columns contain data. Once you know this you can derive the bottom row.
To quickly go through the returned values, I would suggest using sheet.getDataRange().getValues() which will return a 2 dimensional array with all the values stored in your sheet.
To know the last row containing data you can then easily do the following:
var lastRow = sheet.getDataRange.getValues().length;
Note that since the length of an array starts counting at 0 for the first row whereas when you want to set values back to your sheet your row count will start at 1. This means you need to add 1 to the lastRow count.
Now that you know the last row you can do some cleanup first to remove a previous bottom row with sheet.deleteRow(lastRow+1);
To then add a new bottom row you can simply use the append function where you pass an array with all the values for all cells of the row like this:
sheet.appendRow(["cellInColumnA";"cellInColumnB","cellInColumnC"]);

Related

Pull Entire column from different sheet without zero's

how to pull column from different sheet..
I've a sheet using the below formula to get the column(n rows) from Sheet1 to Sheet2 however I'm getting zeros after nth row. How to avoid zeros here ??
=IF(NOT(ISBLANK(Sheet1!C1)),Sheet1!C:C,"")
I'm doing the same for A,B,C columns to get data from Sheet1 to Sheet2. Any better formula ?
zeros are coming because the original column doesn't have data after nth row. for example if zero occur at 301 for column c means data in the column C has values upto 300 rows.
we can avoid this issue in two ways
stop copying formula after you get zeros
or
Put a unique word in the formula (here stopcopying)
=IF(LEN(IF(NOT(ISBLANK(Sheet1!C12)),Sheet1!C:C,""))=0,"stopcopying",IF(NOT(ISBLANK(Sheet1!C1)),Sheet1!C:C,""))
Find "Stopcopying" keyword and replace without giving any word ( ctrl F and R)
This is the only way I can see and this answered my question:
=FILTER($B:$E,(B:B<>""))

How to specify the range of all rows containing data in Google Sheets

What is the syntax for selecting the full range of rows which contain data?
Say for instance I have 200 rows of data. Im running some functions which loop through rows. If I wanted to select the range of these rows manually from column A, I would write is as A1:A200.
But is there a way to write it from "A1" : "last row in the column A that contains an entry"
Alternatively is there a way to write A1:"end of column A, regardless of entries"?
thanks
You can write A1:A to get the entire column, or A1:Z to get a range of columns. In Apps Script, you can call sheet.getLastRow() (or sheet.getLastColumn()) to get the last row containing data and use that to write your loops.
References
sheet.getLastRow()
sheet.getLastColumn()
You can also consider the use of getDataRange().This method returns a Range corresponding to the dimensions in which data is present.

get the count (integer) of cells in a specific column that have a value, with google apps script

Is there a way to get the count of cells that have values in a specific column?
I need it for 2 reasons.
First off I want to read that number back to the user in a dialog box.
secondly I'm running a function multiple times with the forEach method.
I don't want to run it more times than the amount of cells with values, but can't seem to
set the range according to the values count.
keep in mind this column's data is dynamic.
It's not the same amount every time it depends on the user's input.
Note: calling getDataRange() or getLastRow() don't help because they return the entire sheet's count
which can be more than the column I need.
function getCount() {
let a=SpreadsheetApp.getActive().getActiveSheet().getDataRange().getValues().map(function(r){return r[0];}).filter(function(e){return e;});
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(a.length), "count")
}

Find Row Where Sum is Reached from Single Joined Column (not a range of cells)

I'm trying to run a formula to identify in which row a total sum is reached.
I've been able to do that calculation when I have an entire range of cells to work with, however, I'm doing a filter / join calculation because I need to do this from an individual row with all the data instead of an entire range of cells.
Here is an example google sheet (EDITABLE - feel free) where you can see the range and working formula (both below). Help getting this from the single-cell versions on the top would be very helpful. The error I get with both row() & index() formulas is that the "argument must be a range".
If there's another way to do this besides the single-cell I had that doesn't require referencing the range (e.g. using FILTER) then I'm open to it.
My desired result is to be able to pull the get the second column (date) at the point when the sum is reached (can be via the INDEX & MATCH formula I used or an alternative). This will tell me the earliest date that feeds into the desired sum.
Yes unfortunately you can't do that trick with SUMIFS to get a running total unless the column being totalled is an actual range.
The only approach I know is to multiply successive values by a triangular array like this:
1 0 0 ...
1 1 0 ...
1 1 1 ...
so you get just the sum of the first value, the first 2 values, then 3 values up to n.
This is the formula in F5:
=ArrayFormula(match(E14,mmult(IF(ROW(A1:INDEX(A1:ALL1000,COUNT(split(A5,",")),COUNT(split(A5,","))))>=
COLUMN(A1:INDEX(A1:ALL1000,COUNT(split(A5,",")),COUNT(split(A5,",")))),1,0),TRANSPOSE(SPLIT(A5,",")))))
And the formula in F6 is just
=to_date(INDEX(TRANSPOSE(SPLIT(B5,",")),F5,1))
EDIT
You might have guessed that the above formula was adapted from Excel, where you try to avoid volatile functions like Offset and Indirect.
I have realised since posting this answer that it could be improved in two ways:
(1) By using Offset or Indirect, thus avoiding the need to define a range of arbitrary size like A1:ALL1000
(2) By implying a 2D array by comparing a row and column vector, rather than actually defining a 2D array. This would give you something like this in F5:
=ArrayFormula(match(E14,mmult(IF(ROW(indirect("A1:"&address(COUNT(split(A5,",")),1)))>=
COLUMN(indirect("A1:"&address(1,COUNT(split(A5,","))))),1,0),TRANSPOSE(SPLIT(A5,",")))))
which could be further simplified to:
=ArrayFormula(match(E14,mmult(IF(ROW(indirect("A1:A"&COUNT(split(A5,","))))>=
COLUMN(indirect("A1:"&address(1,COUNT(split(A5,","))))),1,0),TRANSPOSE(SPLIT(A5,",")))))

getActiveRange returning a smaller range than expected

I have a simple spreadsheet table with 20 columns, with an application that update rows on the first 14 columns. When one row is modified I am trying to capture the row using getActiveRange(). But I am getting 12 columns instead of the expected 14. No error. Just less columns than expected.
var ssPlat= SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Plateau");
var aValues= ssPlat.getActiveRange().getValues();
var aCol = ssPlat.getActiveRange().getLastColumn()
Logger.log("Columns:"+aCol+":"+aValues)
I don't want to hard code the range because more columns may be added later.
Please help.