Apps Script - count rows in variable range - google-apps-script

With Apps Script, I need to copy a section of a sheet to another area, with the start and end cells of the range based on variable inputs. Is there any built in function to count the number of rows in this variable-sized range?
I've searched all over and can't find any way to get this seemingly simple info for a defined range.

.getValues() on a Range object returns a regular javascript array of arrays, which you can measure with .length.

Related

I need a Google Sheets script & formula regarding looking up a dynamic value from a range of cells and using that value in a formula elsewhere?

I would like to have a script that runs upon change in values to the sheet. What I have setup on my sheet is a few cells in a column that can have dynamic values typed into one of them (ex. J6:J10). I would like the script to run and check for a dynamic value in one of the cells, which it would then use that value in a formula contained in a different cell (I6) to calculate a formula similar to this: =ROUND(J6/E6,2). Cell J6 being dynamic.
I need the formula to be dynamic to match whichever J cell row has data and correlate to the E cell row
I was only able to get the sheet setup with its basic layout. I am still missing a working script that functions with the proper dynamic formula
If only 1 of the cells in J6:J10 has the value which you want to work with you could use in I6:
=ROUND(SUM(J6:J10)/E6,2)
What about this? It calculates all the ratios, and FILTER if J is empty; and finds the minimum ratio with MIN:
=IFNA(MIN(FILTER(INDEX(J6:10/E6:E10),J6:J10<>"")))

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")
}

Counting rows in a Google sheets script

I am building a menu system with date and food columns. The screen will have a [working date] cell that controls which rows show on the screen.
When the [working date] changes the script should run and :
count the number of rows on the sheet with a date that matches the [working date]
count the number of row with matching date and blank food cells
add enough rows plugged with the date to fill the sheets screen
add additional rows if needed to allow additional entry by scrolling the screen
sort all the rows
scroll the sheets screen so that the first row for the selected date is at the top
This should all happen automatically when the user changes the [selected date.]
I am completely green on working in scripts and initially thought I could use the dcount function to get the counts. I now believe that the sheet side functions can only be used from there. I expect that in a script one will need to select a range of data and loop through it to get the counts.
I am pretty sure I could eventually figure this out but would really benefit if someone could give me a few pointers.
Thanks in advance - Joe
For the whole process you would need to use an onEdit trigger to execute a function each time the sheets is edited and use it's event information to conditionally run the code when the [working date] cell is the one edited. I would use the installable version of the trigger which has less restrictions.
For all the actions you want to achieve you'd have to use the SpreadsheetApp methods.
1 and 2) Create a TextFinder object from a Spreadsheet object and set it up to make the search. When you set up the range for the text finder, avoid using this version of the getRange method or it may throw an error.
3 and 4) With insertRows method you can insert rows to the Sheet object, and then use its getRange and setRange methods to set the values. There's no trigger available you could use to run a function when the user scrolls the view.
5) You can use the sort method. Or get the range values, sort them with JavaScript methods and then set the ordered array in the range.
6) You can use the activate method of a Range object which will put the focus in the range cell(s).

How to define the first row number of a Named Range? (Google Sheets)

In Google Sheets Script Editor, I am trying to identify how to use getRow() or equivalent statement to be able to output the row number of the first row in a Named Range.
For example, I have a named range showHide from C4:C10. What would I use to output "4" since that is the first row of my range?
getRow, getValue and similar functions applied to ranges always output the top left cell's content.
So for you this does the job:
SpreadsheetApp.getActiveSpreadsheet().getRangeByName("showHide").getRow();

Google Spreadsheet Populating Cells in Other Sheet Based on Value

Am new to Google Docs, but have to create a cumulative report of comments that are flagged as positive or negative. I have 6 worksheets that ideally would populate to a single report, but I could create 6 individual reports for now.
In the source sheet, ColA is a numeric code identifying the category. Col B is the category description; Col C are the notes from one person; Col D is the code to identify it as positive or negative; Cols E and F are the notes from a 2nd person; G/H from a 3rd, etc.
The report sheet needs to transpose the vertical comments by category with the positive comments for all persons for the first category in Col G, the negative comments for the 1st category in Col H, etc for all 6 categories.
I was able to manually create this report using the following formula to extract the Positive comments from column C:
QUERY(EntrySheet1!C5:D15;"select * where D='P'")
But, it's pretty tedious to copy the formula laterally and vertically to accommodate all 6 categories and all 6 note takers.
So, my questions are whether or not there is an easier way to extract the information the way I need to report it. Also, is there a way to use something like Excel's Indirect function where I could use the concatenate function to build the formulas and the Indirect to evaluate that function. My thought here is that I could have an entry cell where I would identify which cumulative report I wanted to view by simply updating the cell. An alternative would be to load the data into an array and use a script to populate a static cumulative report. Real-time updating with formulas would be ideal, but creating a static report that is created from a script is acceptable. My biggest concern is the manual effort to update the formulas since they are sheet specific.
Use Google Spreadsheet INDIRECT function.
See the Google spreadsheets function list:
INDIRECT(reference)
Returns the reference specified by a text string. This function can also be used to
return the area of a corresponding string. Reference is a reference to a cell or an
area (in text form) for which to return the contents.
You might be able to feed the results of indirect into your query.