Google script to delete rows - google-apps-script

when I use the deleteRows function I get the error :
TypeError: Cannot read property 'deleteRows' of null
at clearRange(Code:4:4)
here is my code:
function clearRange() {
var ss = SpreadsheetApp.getActive();
// Rows start at "2" - delete of N rows (400)
ss.deleteRow(2, 400);
}
Can you please help me to solve this issue?
Thanks and regards
Thierry

As Andres has said, your script may not be attached to your sheet, and you will have to go to the sheet in question and click Tools > Script Editor.
However, there are two other possible issues -
The function is named deleteRows and not deleteRow
You're calling deleteRows() on a Spreadsheet object. It is a Sheet function.
If these were the issues, this code should fix it.
function clearRange() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
// Rows start at "2" - delete of N rows (400)
sheet.deleteRows(2, 400);
}

Your ss spreadsheet variable is null which means your script is not bound to any spreadsheet as it should be to use the getActive method. You can create a container bound script following the steps explained here:
To create a bound script, open a Google Sheets, Docs, Slides, or Forms
file, then select Tools > Script editor.

Related

Data validation in Google Spreadsheets

I tried a script that is example of google script for data validation. I just copied - paste the script below, could you help please?
function validateMySpreadsheet() {
// Set a rule for the cell B4 to be a number between 1 and 100.
var cell = SpreadsheetApp.getActive().getRange('B4');
var rule = SpreadsheetApp.newDataValidation()
.requireNumberBetween(1, 100)
.setAllowInvalid(false)
.setHelpText('Number must be between 1 and 100.')
.build();
cell.setDataValidation(rule);
}
But I got the following error:
Cannot read property 'getRange' of null
The reason you are receiving the Cannot read property 'getRange' of null is because most likely you don't have an active sheet available - meaning the script you are using is not bound to any spreadsheet.
In order to fix this, I suggest you make the following changes to your code:
var spreadsheet = SpreadsheetApp.openById('SS_ID');
var sheet = spreadsheet.getSheetByName('SHEET_NAME');
var cell = sheet.getRange('B4');
I also suggest you take a look at the below links since they might be of help to you.
Reference
Apps Script Container-bound Scripts;
Apps Script SpreadsheetApp Class - openById();
Apps Script Spreadsheet Class - getSheetByName().

Google Sheets Timestamp on change

What I'm trying to achieve is really simple however I'm super uncomfortable with the Google Sheet Script Editor.
I want the date in a certain cell updated whenever something changes on the active sheet.
That's what I got so far:
//Timestamp on change
function timestampOnChange() {
getActiveCell().setValue(new Date());
}
However, even that already returns error messages :/
Explanation:
You need to create an onChange() trigger of a particular
function which will update a specific cell of the active sheet when the spreadsheet
changes content or structure.
The updateCell() function updates the value of B1 with the current
timestamp of the active sheet.
I would advice you to update a specific sheet instead of the active
sheet by using:
const sh = ss.getSheetByName('Sheet1');
instead of:
const sh = ss.getActiveSheet();
Solution:
In more detail, The createOnChangeTrigger() function will create
the onChange() trigger that will execute updateCell() when there
is a change in the content or structure of the spreadsheet file and in our case the active sheet.
function createOnChangeTrigger(){
const ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger("updateCell")
.forSpreadsheet(ss)
.onChange()
.create();
}
function updateCell(){
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
sh.getRange('B1').setValue(new Date());
}
Instructions:
Copy/Paste both functions in the script editor and execute createOnChangeTrigger() only once.
References:
onChange()
From the question:
//Timestamp on change
function timestampOnChange() {
getActiveCell().setValue(new Date());
}
However, even that already returns error messages
The above code returns an error because theres isn't a built-in Google Apps Script function named getActiveCell(). To get the active cell you could use
SpreadsheetApp.getCurrentCell()
or
SpreadsheetApp.getActiveRange()
or if you use an edit event object (let say that it's a assigned to e) you could use e.range
It's worthy to not that if you use setValue(new Date()) on the active cell what ever that was entered on that cell will be overwritten.
On Web Applications, a sister site of Stack Overflow, there a lot of Q/A about timestamps in Google Sheets, actually, there is tag for them ---> https://webapps.stackexchange.com/questions/tagged/google-sheets-timestamp. So far there are 102 questions with this tag.

How can I get a macro to automatically trigger when a cell reaches a certain value in a Google Sheet?

What I need is for a macro I've recorded called SwitchHotSeat to trigger when the value of cell F3 goes above £1,000,000.00 0r 1000000 or the LEN of cell F3 goes over that length.
I can find guides for doing this in Excel, but not for Google Sheets. below is just the code for my macro.
function SwitchHotSeat() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('3:3').activate();
spreadsheet.getActiveSheet().deleteRows(spreadsheet.getActiveRange().getRow(), spreadsheet.getActiveRange().getNumRows());
spreadsheet.getActiveRangeList().setBackground('#ff0000')
.setFontColor('#ffffff');
};
The macro works fine. I just need a way of triggering it when that cell goes over the 1000000.
You could use simple or installable triggers. See
https://developers.google.com/apps-script/guides/triggers
https://developers.google.com/apps-script/guides/triggers/installable
Example of using onEdit simple trigger
function onEdit(e){
if(/* add here your conditions */) {
SwitchHotSeat();
}
}
NOTE: on edit and on change triggers are triggered only when an user edit the spreadsheet.
Just in case it helps anyone else with a similar issue, I discovered (by logging) that it was returning an object, rather than a number before, so the following code worked fine in the end.
function onFormSubmit(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var millionaire24 = ss.getSheetByName('Millionaire24.');
var cellValue = millionaire24.getRange(3,6,1,1).getValues();
Number(cellValue)
if(cellValue[0] >= 1000000)switchHotSeat();
}

Edit a Google Spreadsheet using Google Apps Script

I am trying to edit a google spreadsheet using google apps script. Basically, I have a cell in the spreadsheet that has the value "Pending Approval" in it. When the script is executed, I want it to change the value in that cell to "Approved". Is there a simple way to do this?
.getRange and .setValue are the methods you will want to use in whatever you are trying to do. This little function will just take the cell notation as the first argument, and then the value you want to set as the second.
function setCellValue(cellName, value) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = sheet.getRange(cellName);
cell.setValue(value);
}
setCellValue("A1","Approved");

Google Scripts Sheets hide/ unhide

In writing a 'custom function' Google Script for my particular sheet, I simply want to hide a column:
function hideColumn(index) {
// get active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// get first sheet
var sheet = ss.getSheets()[0];
sheet.hideColumns(index);
}
This code works fine when I run it from within the Script editor but if I try to run it from inside a cell "=hideColumn(2)", I get the following error:
"You do not have permission to call hideColumns (line 48)."
From the same sheet/ script I'm able to run other custom functions such as:
function metersToMiles(meters) {
if (typeof meters != 'number') {
return null;
}
return meters / 1000 * 0.621371;
}
This seems to be some issue with the hideColumns function being run from inside a sheet? (ie. custom function?)
Your script 'hideColumn' is not a custom function, but a 'normal script'. Also it does not return anything (whereas the second function does). Only custom functions can be entered like formulas in the spreadsheet. See here for more info. My advice would be to create an extra menu-item using an onOpen trigger so you can run the function from the (spreadsheet)menu.
Hope that helps ?