ClearRange automatically
ClearRange automatically
I want to clear a specific range of my Google Sheet twice a day by triggering this function.
My script is as follows:
function clearRange()
var sheet = SpreadsheetApp.getActive().getSheetByName('test');
sheet.getRange('inventaire!B3:E8').clearContent();
But I get
TypeError: Cannot call method "getSheetByName" of null. (line 3, file "Code")
I don't know why I receive this error.
If this script is not bound to a spreadsheet, then the call to
SpreadsheetApp.getActive()
will return null
. This is stated in documentation. developers.google.com/apps-script/guides/bound developers.google.com/apps-script/reference/spreadsheet/…– tehhowch
Aug 28 at 20:58
SpreadsheetApp.getActive()
null
1 Answer
1
I would suggest you pass as parameter the name of the sheet where belongs the range in this case inventaire
not test
, by the way it appears your app is not linked to a spreadsheet as getActive
returns null
. Try to make sure your script is linked to your spreadsheet try also the getActiveSpreadsheet()
method then write.
inventaire
test
getActive
null
getActiveSpreadsheet()
function clearRange()
var sheet = SpreadsheetApp.getActive().getSheetByName('inventaire');
sheet.getRange('B3:E8').clearContent();
Ah yes, of course. test is the name of the document, but inventaire is the name of the sheet (and not the tab). It works. Thanks
– user3680228
Aug 28 at 20:20
can you validate as answered please?
– JSmith
Aug 28 at 20:26
Note that this doesn't resolve the OP's error. OP's error clearly states that the object on which
getSheetByName
is called is null
- it is not an instance of a Spreadsheet
.– tehhowch
Aug 28 at 21:02
getSheetByName
null
Spreadsheet
because test is not a sheet it is the spreadsheet, so this solves the problem. There Imagine test was the sheet there would be no logic in calling the range of another sheet in the
test
sheet object. So you clarify the logic and errors from user seems more clear– JSmith
Aug 28 at 21:06
test
if OP breaks up the code to this:
var spreadsheet = SpreadsheetApp.getActive(); var sheet = spreadsheet.getSheetByName("some name");
, the error OP is getting indicates that the value of spreadsheet
will be null
. It doesn't matter what the name of the sheet being requested is, because the spreadsheet being acted on does not exist– tehhowch
Aug 28 at 21:39
var spreadsheet = SpreadsheetApp.getActive(); var sheet = spreadsheet.getSheetByName("some name");
spreadsheet
null
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
is it working now?
– JSmith
Aug 28 at 19:56