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.





is it working now?
– JSmith
Aug 28 at 19:56





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 getActivereturns 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 testsheet 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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế