GAS : How to format all empty cells in a sheet - google-apps-script

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.

Related

Edit cell above cell that called a function?

I'm using Google Apps Script to make a function that will be called in a Google Sheet. The function does some auto calculating for a spell effect. Some spells need the player to provide extra info (How many people are you shielding, etc) and some don't need any additional inputs. If extra info is needed, the user provides it in the cell directly above the cell that calls the function. I'll call this the "input cell" for clarity. The "function cell" is the one that calls the function, directly below the input cell.
My question is this: if the extra info is unneeded, as determined by conditionals within the function, how do I overwrite the contents of the input cell with '-'? I can't put a formula within that cell directly, because the user would overwrite the formula when entering the additional information. The function also returns a value to the cell that called it.
I've tried a bunch of techniques but I can't figure out how to do it.
I tried returning an array of both values, but it flows downwards instead
of upwards, and I couldn't find anything for "return array offset
upwards" or something to that effect.
I can't use absolute references
like "L17" because the function can be called from many places on the
sheet, and it also only needs to mess with the cell directly above it.
using setValue() or setValues() threw a "no permission" error.
I would attach my code, but it's a mess of half-deleted attempts that didn't work, and wouldn't be much help at all. I also don't want to ask anything too specific, for fear of running into the https://xyproblem.info problem. I just want to be able to have a function edit the cell directly above it, and I don't care about which technique gets that accomplished.
Any help is greatly appreciated. Thank you!
No cell can contain both a formula(or a output of a formula) and simultaneously contain a user entered value. That was always the case with any spreadsheet, Excel or Google sheets or any other. It's one or the other. Choose one and redraw your logic!
For your specific problem, if you can reflow your logic, you might try simple triggers like onEdit.

ArrayFormula is affecting getLastRow

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.

Applying formulas on cells in separate Google sheets

I have two different Google spreadsheets (these are separate files). I would like to calculate the sum of two cells (say A1) in the first and second spreadsheet/file and have it show up in a third/new spreadsheet/file. Is this possible and how might one go about doing this? Thank you.
you can use the IMPORTRANGE formula, which is ("URL of other sheet","range of cells you want to import").
Make sure both parts of the formula have " around them, so that the formula in the 3rd sheet would be something like:
=IMPORTRANGE("Sheet1Key","Sheet1!A1")+IMPORTRANGE("Sheet2Key","Sheet1!A1")

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);