Testing for empty cell - google-apps-script

I am super new to Google Apps Script, and the Java it's based on. In fact I just dived into the deep end of the pool not even eight hours ago! I know VBA, SQL, etc, but I digress.
I've figured out how to change the background color of row when the cell contains a certain value. I can also change the color if the value changes to something else. What I can't do is unchange the color if the value is deleted.
How do I use google apps script to test for a blank cell?

Try this:
function getMyColorValue()
{
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('My Sheet Name');
var rg=sh.getRange('A1');
var colorValue="#ffffff";
if(!rg.isBlank())
{
colorValue=rg.getValue();
}
rg.setBackgroundColor(colorValue);
}

Related

Show and hide selected columns with single macro in google sheets

Starting out like all others. I am new to macros and I know what I'm trying to do is easy, but I can't get it working.
So I'm making buttons on a spreadsheet to show and hide columns, if the column is hidden then show, if the column is shown then hide. So a toggle button between show and hide basically.
I will continue to research while this is here as I'm sure the answer is simple. Just an If, then, else but I'm a noob.
Any help would be appreciated. Here's one of the 'show' ones I have if it's easier to edit that.
Thanks
function ShowCA() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getActiveSheet().showColumns(6);
};
I believe your goal is as follows.
About the column "F", when the column is showing, you want to hide the column. When the column is hiding, you want to show the column.
You want to achieve this using a function of Google Apps Script.
In this case, I thought that isColumnHiddenByUser can be used. So, how about the following sample script?
Sample script:
function ShowCA() {
var column = 6; // This is column "F".
var sheet = SpreadsheetApp.getActive().getActiveSheet();
if (sheet.isColumnHiddenByUser(column)) {
sheet.showColumns(column);
} else {
sheet.hideColumns(column);
}
}
When this script is run, when the column "F" is showing, the column is hidden. When the column "F" is hiding, the column is shown.
References:
isColumnHiddenByUser(columnPosition)
showColumns(columnIndex)
hideColumns(columnIndex)

Trying to run macro to add formula onto the end of an entire google sheet column

I have recorded a macro that adds formula onto the end of a cell
function addpricemultiplier() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('H5:1000').activate();
spreadsheet.getCurrentCell().setFormula('=0.3*GOOGLEFINANCE("CURRENCY:USDAUD")');
spreadsheet.getRange('H5:1000').activate();
};
The problems I have are that it will only change if the value is =0.3, I am unsure how to change it so that it will add the formula on no matter what, and of less importance I have made the macro from H5 onto 1000, I am unsure if there is a way to make it just go on as long as the sheet is there instead of pre determining where it will go to.

Script to set default font size and style in google sheets

I have a shared google sheet where people constantly paste new data in however the data is always in different styling and restyling it is very time consuming. I was wondering if it it's possible with a script that auto correct styling and size in a document.
If you are looking at resetting it to the default theme then you can use this script:
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSheet();
var range = ss.getRange(1, 1, ss.getMaxRows(), ss.getMaxColumns());
range.clearFormat();
}
This triggers every-time a user edits the sheet (including pasting some data) and clears all formats in the sheet. If this doesn't work for you in case you have specific formats for some cells, like data headers that you don't want to change, then you can customise the script using methods like setFontSize(size) etc.
Edit: Changed the answer for the specific spreadsheet.
Use this script
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSheet();
var range = ss.getRangeList(['B29:B42', 'B27']);
range.setFontFamily("Arial")
.setFontSize(11)
.setFontColor("#000000")
.setBackground("#ffffff");
}
The following script will reset the formatting on the edited range. You can further validate the row/column/sheet before resetting the formatting (eg. to prevent header rows from getting affected) using the values of e.range or e.range.getSheet()
function onEdit(e) {
e.range.setFontColor() // Resets color
.setFontSize(10) // Sets size to 10 (has to be a number)
.setFontFamily() // Resets font family
.setFontLine() // Resets underline, strikethrough
.setFontWeight() // Resets bold
.setFontStyle() // Resets italics
}
Instead of onEdit, some other options include
making a timed trigger that routinely resets the formatting
running a script manually when you would like to reset formatting
Refer to Range class.

How to automatically replace a word with another in Google Sheets using JavaScript?

Could someone please write a code that automatically replaces a word with another whenever it is entered or chosen in Google Sheets.
There's a drop drown list for a column in the spreadsheet that I'd like to use the code for. So whenever a specific name is chosen from that drop down list, it automatically changes to something else.
Is this even possible? Please help
You could do this without going into script editor.
Just do in the cell you want to automatically change
= CELL WITH LIST
So an example would be if you want a cell to change then simply just do something like
=F6
Here's your code:
function onEdit(e)
{
var sh=e.source.getActiveSheet();
var col=e.range.getColumn();
var row=e.range.getRow();
if(sh.getName()=='Sheet57' && col==1)
{
sh.getRange(row,col).setValue('horse');
}
}

Script to Change Cell Color if any changes occurs

I'm working on a Google spreadsheet that is edited by multiple people. I want to highlight the cell background color in red if any changes occurs in column 4-26 for multiples sheets.
Please Note:
In column 4-26 there may be value in number or text if it get changed from its original value or text then only we need to highlight it
Set up and onEdit trigger in your project and select this function.
function onEdit1(e)
{
var range=e.range;
var column=range.getColumn();
if(column>3 && column<27)
{
range.setBackground('#ffff00')'
}
}