ClearSheet function not working on all tabs of spreadsheet - google-apps-script

I have a Google sheet that needs certain cells to be cleared at midnight every night. It has 3 tabs titled 'DOV', 'EMD', and 'Taylor's sheet', and on each of these sheets the same range needs cleared (D6:D100).
I used 'Taylor's sheet' as a test sheet because I haven't coded in forever and wanted to to make sure everything worked well first. So I implemented the clearContent() for the designated range and everything came out fine. However, once I tried adding the 'DOV' and 'EMD' sheets like my original test sheet I keep getting errors regarding the getRange function.
function ClearSheet1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Taylor's Sheet");
sheet.getRange("D6:D100").clearContent();
var ss1 = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss1.getSheetByName("EMD");
sheet1.getRange("D6:D100").clearContent();
var ss2 = SpreadsheetApp.getActiveSpreadsheet();
var sheet2 = ss2.getSheetByName("DOV");
sheet2.getRange("D6:D100").clearContent();
}
I'm confused why using getRange is returning an error
"Cannot call method 'getRange' of null."
Do I need to break each of these out into its own function?

Related

copyInfo from one sheet to another sheet in google sheet with script

I want to copy some data from sheet 1 to sheet 2, but every time I tried to run the below-attached code it send me an error which I mention in the comment.
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Sheet 1");
var pasteSheet = ss.getSheetByName("Sheet 2");
// get source range
var source = copySheet.getRange(2,1,38,17);
// get destination range, "TypeError: Cannot call method "getRange" of null."
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,38,17);
// copy values to destination range
source.copyTo(destination,{contentsOnly: true});
}
Thanks in advance.
Your code seems correct and is working for me,
Can you try to run this code and check if its logging the correct name of your google sheet that you are using,
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
Logger.log(ss.getName());
}
For better access to a particular google sheet you can use the following method to access the file rather than using getActiveSpreadsheet
var ss = SpreadsheetApp.openById("your sheet id");
You can even try the debug window to check what is the present values of different variables and get some idea of possible errors.
The error you are receiving is due to the fact that pasteSheet is null.
This is most likely due to one of the following reasons:
your spreadsheet does not have any sheet named Sheet 2 - I suggest you double check the name of the sheets from your spreadsheet;
the active spreadsheet you are retrieving is not the one you are needing for copying the data;
As suggested above, you can indeed make use of the openById method in order to retrieve the needed spreadsheet and afterwards get the actual sheets.
Reference
openById(id);
Apps Script Troubleshooting.

getActiveCell() always returns 1,1

I know this has been asked before but none of the answers seem to be working for me. I need to get the values of cells that are clicked on. As a very basic prototype, I created a script tied to my spreadsheet with the following:
function getVal() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
Logger.log(sheet.getActiveCell().getValue());
}
I then go into my spreadsheet and select a cell with a value and run the function in the script window. No value is displayed in the log.
Next I tried:
function getVal() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
Logger.log(sheet.getActiveCell().getRow() + ',' + sheet.getActiveCell().getColumn());
}
I then go into my spreadsheet and select a cell with a value (G6) and run the function in the script window. The log displays 1,1.
And finally I tried
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
Browser.msgBox(sheet.getActiveCell().getRow() + ',' + sheet.getActiveCell().getColumn());
}
I then go into my spreadsheet and select a cell with a value (G6) and make a change to it. When I hit enter, the popup dialog displays 1,1.
This seems like it should be really simple but for the life of me cannot figure it out. Can anyone help?
I tested this in a spreadsheet and I found that using var sheet = ss.getActiveSheet(); yielded the correct results whereas using the .getSheets()[] method did not.
This is a bit late, but I was researching this today, and wanted to share: My issue was similar, in that sheet.getActiveRange().getRow(), sheet.getActiveCell().getRow(), etc., all returned row 1, regardless of what cell or range was active.
In this case, I was opening the script from a previous bookmark I created. Opening the script from a bookmark apparently doesn't re-establish the active relationship with the sheet.
Once I opened the script from the spreadsheet instead, my code returned the correct row reference.
I have tried following code and works fine. getRow() method is of Range class not Sheet class.
var ActSpSheet = SpreadsheetApp.getActiveSpreadsheet();
var ActSheet = ActSpSheet.getActiveSheet();
var GetCell = ActSheet.getCurrentCell();
var GetCurRow = GetCell.getRow();
Logger.log(GetCurRow);

No Longer Able to Access Spreadsheet Via URL

The below code used to work perfectly to get a range from one spreadsheet and use them as the values for a range in another spreadsheet. It will no longer work. I have tried openByUrl functions in other scripts and they will not execute, either. Error message that debugging gives me is that openByUrl cannot be found in object SpreadsheetApp. Any idea why?
function IMtasks(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var tss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/##############/edit#gid=#########');
var tasks = tss.getSheetByName("TASKS");
var tasksrange = tasks.getRange("A3:F102").getValues();
sheet.getRange("A202:F301").setValues(tasksrange);
}

After Three Days of Experimenting and Searching, I STILL Can't Get .getValue Function to Work

Sorry for the long subject line, but this site wouldn't accept a more concise statement.
I have a Google Sheet, that has nothing more than my name in Cell A1.
Then I went to Tools / Script Editor, and have a "Code.gs" script that says:
function onEdit(e) {
Logger.log("We're in the function.");
var ss = Spreadsheet.App.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var valueOfCell = sheet.getRange(1,1).getDisplayValue();
Logger.log("Value of Cell is '" + valueOfCell + "'.");
}
When I edit my very simple sheet, and check the log (View / Logs), I get:
[19-02-28 08:58:10:182 CST] We're in the function.
That's it. I've tried every permutation I can think of or suss from three days of web-searching, and I simply can not make the .getValue() (or .getDisplayValue()) work.
What am I doing wrong?
Thanks!
/Kent
"Spreadsheet.App" should be "SpreadsheetApp".
Additional permutations that work:
Logger.log("We're in the function.");
var ss = SpreadsheetApp.getActiveSheet();
var valueOfCell = ss.getRange(1,1).getDisplayValue;
and
Logger.log("We're in the function.");
var valueOfCell = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(1,2).getDisplayValue();
and
Logger.log("We're in the function.");
var valueOfCell = SpreadsheetApp.getActiveSheet().getRange(1,2).getDisplayValue();
After four days, I think I finally understand some of this. For others as dense as I, I leave this documentation here:
Spreadsheet is the "workbook", the collection of Sheet tabs
Sheet is the individual tab'd sheet in a Spreadsheet "workbook".
Seems like simple enough information, but I couldn't find this documented anywhere.
SpreadsheetApp is the Class, the "parent" of all the Spreadsheets. While probably not technically accurate, for purposes of my thinking, I think of "SpreadsheetApp" as being the "Sheets" application within Google Docs, as opposed to "Docs" or "Slides".
So,
var ThisApp = SpreadsheetApp;
// Sets the variable "ThisApp" to "SpreadsheetApp", which I guess is the name/identification of the spreadsheet app.
var TheActiveWorkBook = SpreadsheetApp.getActive();
// Sets "TheActiveWorkBook" to the identifier for the current collection of sheets (collection of tabs within a spreadsheet).
var TheActiveWorkBook = SpreadsheetApp.getActiveSpreadsheet();
// Identical to above; just a different name for it.
var ActiveWorkBookName = SpreadsheetApp.getActive().getName();
// Sets the variable to the *name* of the workbook/spreadsheet (like, "Fred's Spreadsheet").
var ActiveSheetInWB = SpreadsheetApp.getActiveSpreadsheet();getActiveSheet();
// Sets the variable to the identifier of the active tab in the spreadsheet.
var ActiveSheetInWB = SpreadsheetApp.getActive();getActiveSheet();
// Identical to above.
var ActiveSheetInWB = SpreadsheetApp.getActiveSheet();
// Identical to above. The active sheet is the active sheet, regardless of whether we know the active spreadsheet, so the extra step of including the active spreadsheet, as we did in the previous two examples, is unnecessary. (The system knows that the active spreadsheet is the one that contains the active sheet.)
var ActiveSheetInWB = SpreadsheetApp.getActiveSheet().getName();
// Gets the *name* of the sheet, as it shows on that sheet's tab.
var sheet = SpreadsheetApp.getActiveSheet();
var ActiveSheetInWB = sheet.getName();
// Shortens the long "SpreadsheetApp.getActiveSheet()" to a short reference to save typing in subsequent uses. Otherwise identical to above example.
I think I'm finally getting the hang of this....

getActiveRange not returning current selection

This should be a simple one, but I could not crack it myself...
I want to copy the currently selected cells in the active sheet in an array called data:
var sheet = SpreadsheetApp.getActive().getActiveSheet();
var selection = sheet.getActiveRange();
var data = selection.getValues();
The above results in the array getting the content of cell A1, no matter which cells are actually selected. When I replace getActiveRange() with e.g. getRange(2,1,10,10) that works as expected.
Any clue as to why I cannot get to the currently selected cells is appreciated!
It looks like the problem was on Google's side because after 24 hours of failure the existing code now works flawlessly. All your versions work fine too now.
I think that the problem is
var sheet = SpreadsheetApp.getActive().getActiveSheet();
This because there is some kind of bug when "chaining" methods of two different Object Classes, in this case Class Spreadsheet and Class Sheet. There is a bug report related to this getActiveSheet() returns first sheet name
The workaround is to replace the above line with:
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
Related
Why Class Range getValues sometimes returns [[]] when chained to Class Sheet getActiveRange?
This gets and displays the active range in a dialog window.
function getARange(){
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var rg=sh.getActiveRange();
var vA=rg.getValues();
var s='';
for(var i=0;i<vA.length;i++){
s+=Utilities.formatString('<br />%s', vA[i].join(','));
}
var userInterface=HtmlService.createHtmlOutput(s);
SpreadsheetApp.getUi().showModelessDialog(userInterface, 'The Active Range')
}