ArrayFormula is affecting getLastRow - google-apps-script

I have this formula =ARRAYFORMULA(IF(B4:B="","",(B4:B-A4:A))) in my spreadsheet.
And also, I have the getLastRow() in the appscript. Apparently the ArrayFormula is affecting the getLastRow(). Is there a possible workaround? Thanks.

ArrayFormula is affecting getLastRow
Yes, that is a feature, it is supposed to change the LastRow because you are outputting an Array all the way to the bottom of the spreadsheet.
Simple fix:
=arrayformula(array_constrain(IF(B4:B="","",(B4:B-A4:A)),max(IF(B4:B="",0,row(B4:B)))-row()+1,1))
What you were originally doing: Outputting an Array with mostly blank spaces all the way to the bottom of the spreadsheet
What this new formula is doing: Outputting only the data you want by restricting the size of the Array to the last row containing data.

Related

GAS : How to format all empty cells in a sheet

I'd like to format all empty/blank cells in a whole sheet with a white background but I don't find the best method to retrieve a range that can handle all those empty/blank cells.
I don't want to create a conditional rule, I'd like to handle it within a script.
What kind of method should I use ?
Thanks a lot !
P.S : I'm a newbie :)
Read the Sheet Class there's two functions that pertain to rows and columns that can give you the exact number of cells in the sheet with one mathematical operation.

Using a Script to Hide a Row Dynamically in Google Sheets

I would be very grateful for some help.
I have a script that will hide a row in my Google Sheet if the value "1" is entered in a cell in column A.
It works as expected.
It does not work, however, when I set the values of non-selected cells. (I want to change these using a formula). I can see that my primitive script is only assessing the value of the current cell.
Instead, I want to constantly evaluate all of the cells in the range A34:A97.
Any row that has a value of 1 in col. A should be hidden.
Any row that has a value <>1 in col. A should be unhidden.
Can anyone help?
Thank you,
So you're probably using an onEdit(e) function and that only works with user edits. There is no trigger for edits that are performed by cell functions or other scripts.
Reference

Intelligently Fill Down Custom Function With Range Parameter in Google Sheets

I have a range of cells that come color coded (background color) from another system (that exports its data into excel format, which I have imported into Google Spreadsheets/Sheets) and I need to make simple calculations based on the background color of each cell.
Unfortunately, the only way I've found in Sheets to determine the background color of a cell is through the use of a script, which I have no experience with.
I have been using the following script:
function getBGColor(range) {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
return sheet.getRange(range).getBackgroundColor();
}
My issue is that the range must be entered in quotes. My understanding is that the value of the cell is passed to the function if the range is entered without quotes. Correct?
=getBGColor("E2")
Sheets doesn't intelligently fill down values in quotes. When I fill down, the formula does not increment as I would need it to (there are hundred of cells I need to get the color of).
I get
=getBGColor("E2")
=getBGColor("E2")
instead of
=getBGColor("E3")
=getBGColor("E4")
I feel like I must be searching using the wrong terminology because I can't find any solutions to this. Do I need to create a custom function that replicates the intelligent fill-down functionality or is there some other solution that I've missed somewhere? Am I attacking the problem incorrectly?
I try to make sheets that I can re-use when new data is available and that can be manipulated on the fly (lots of cell references) to demonstrate the effects of changes in data but it is looking like Sheets may not be the best tool for this? Excel has worked well but my superiors are pushing to move all of this kind of data into Sheets.
Thank you.
Just found something that seems to work. While the
CELL("color",reference)
doesn't work, it did lead me to the address info_type. I was able to use the following workaround:
=getBGColor(CELL("address",E2))
which will intelligently fill down.
If there are better ways to do this please let me know!

Determine the height of merged cells in Google Spreadsheet

I am creating a spreadsheet that will sum the daily inserted hours per activity for each week individually. This spreadsheet allows an increase in height for each week as needed
(ex. week 3 in image below demonstrates another row for inserting a 3rd activity per day).
What I would like to do is find a way to determine the height of the merged cells on the left to be used in dynamically determining the cells to include in a sum total in the last column.
I have found how to determine the existence of merged cells and how to determine the width of merged cells by exporting to HTML, but the latter seems it would not work for my situation as I am looking for a way to do instantaneous height determining to be used in an essentially fancy sum function.
I would like to know if/how it is possible to dynamically determine a merged cell's height using google apps script within google spreadsheet.
Any suggestions or guidance would be appreciated!
I'm assuming that you know where the first row of where the merged cells is. If you know the row position of the merged cell you want to change, I think you can use the getRowHeight() method of the Spreadsheet class, which returns an integer.
getRowHeight Google Documentation
If the problem is, that you can't find the row position of the merged cell you want to change, that's a different question I guess.
You can also set the height:
setRowHeight
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Sets the first row to a height of 200 pixels
sheet.setRowHeight(1, 200);

setNumberFormats not working properly for percentages

I have a Google spreadsheet in which I want to copy values from one range into another range using a script. Copying the values isn't hard; I just use range.setValues(valuesToCopy). I also want to copy the formatting of the original range over to the new range. The problem is that whenever a percentage occurs, it doesn't apply the format correctly and always ends up off by a factor of 100. For example, if the original cell contains a value of 0.5545, the formatting of the cell turns it into 55.45% (which is what I want); however, when I copy the values to another range and apply the formatting with setNumberFormats(), it turns it into 0.5545%. How can I fix this?
EDIT: So, after a bit more fiddling around, it appears that my script properly copies the formatting over the first time, but on subsequent executions of the script, the formatting for the percentages gets messed up, as explained above.
To copy values, use the .copyTo method, and the format is preserved by default. For example,
rangeFrom.copyTo(rangeTo)