What's the difference between getActiveCell and getCurrentCell in Google Apps Script? - google-apps-script

I've read the definitions a few times over and I still don't understand the difference.
getActiveCell: Range
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getActiveCell()
Summary: Returns the active cell in this sheet.
Description: Returns the active cell in this sheet.
getCurrentCell: Range
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getCurrentCell()
Summary: Returns the current cell in the active sheet or null if there is no current cell.
Description: Returns the current cell in the active sheet or null if there is no current cell. The current cell is the cell that has focus in the Google Sheets UI, and is highlighted by a dark border. There is never more than one current cell. When a user selects one or more cell ranges, one of the cells in the selection is the current cell.
There's this note in getActiveCell's documentation: (copy-pasted including grammatical errors)
It's preferrable to use getCurrentCell(), which the returns the current highlighted cell.
Which doesn't make it any clearer

Answer:
getCurrentCell() returns the highlighted cell as seen by the dark border in the Sheets UI, whereas getActiveCell() returns the top-left cell of the selected range.
Example:
Say you have a range highlighted in a Sheet: B2:E10, for example. In the case where the you clicked on B2 and dragged down to E10:
Running the following code in the script editor:
function getCells() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1")
console.log(sheet.getActiveCell().getA1Notation())
console.log(sheet.getCurrentCell().getA1Notation())
}
You will see in the logs that both getActiveCell() and getCurrentCell() return B2.
However in the opposite scenario, where you clicked on E10 and dragged up to B2:
After running the same code, you will see that getActiveCell() still returns B2, but now getCurrentCell() returns E10.

Related

Apps Script: do not recalculate if the cell on the right (next column) is marked as NO - How to get the value of the cell on the right of a give param [duplicate]

I am trying to figure out how to get the A1 Notation of a Cell calling a Custom Function.
I found this but this is for active cell. https://webapps.stackexchange.com/questions/54414/in-google-script-get-the-spreadsheet-cell-calling-a-custom-function
Essentially if I want Cell A5 to Call =TEST(). I want the function to return the text value A5.
I want to use this as a cache identifier for an API Call.
There are subtle differences in different range returning method names- active,current,selection:
The term "active range" refers to the range that a user has selected in the active sheet, but in a custom function it refers to the cell being "active"ly recalculated.
The current cell is the cell that has focus in the Google Sheets UI, and is highlighted by a dark border.
A selection is the set of cells the user has highlighted in the sheet, which can be non-adjacent ranges. One cell in the selection is the current cell, where the user's current focus is.
The first two are still range objects,while the latter is not. To reiterate, getActiveRange()
in a custom function it refers to the cell being "active"ly recalculated.
I want the function to return the text value A5.
Without Custom Functions,We can use:
=ADDRESS(ROW(),COLUMN())
With Custom function,We can use:
function test() {
return SpreadsheetApp.getActiveRange().getA1Notation();
}

Is there any function that takes a value from one sheet, and depending on that value, change the 2nd sheet?

So i want to make a function in google sheets with the general idea:
IF cell in Sheet 2; Row D; VALUE=TRUE Then in Sheet 3,4,5...; the respective cell in row F changes the background and text colour
For Example
Row D, Cell 5 = True in Sheet 2; Row F Cell 5 in Sheet 3 changes to red
Is it possible to achieve this?
You can achieve what you are looking for by using the INDIRECT formula in conditional formatting.
In Sheet 3, select cell F5
Open conditional formatting
Ensure F5 is below Apply to Range
Ensure Custom formula is is below Format cells if...
Enter this formula into the formula bar, which says "look for the value in D5 of Sheet 2. If the value = TRUE, then apply formatting."
=indirect("Sheet2!D5")=TRUE
6. Changing the highlight color to red, and any other formatting available in the menu.

Google Apps Script - Going up from bottom of sheet to an available value

I'm trying to do a macro in google sheets where I select a point in column N (in this case N100) and then simulate control up button to move up to the next available value in column N. But when I do this, I see the cell that's activated is the top of the column (looks like control up is pressed twice).
The code I have is below, any ideas on why this seems to be the case?
function ClearFormq() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Team1");
sheet.getRange("N100").activate()
sheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.UP).activate();
}
Thanks!

Get Spreadsheet cell calling a custom function

I am trying to figure out how to get the A1 Notation of a Cell calling a Custom Function.
I found this but this is for active cell. https://webapps.stackexchange.com/questions/54414/in-google-script-get-the-spreadsheet-cell-calling-a-custom-function
Essentially if I want Cell A5 to Call =TEST(). I want the function to return the text value A5.
I want to use this as a cache identifier for an API Call.
There are subtle differences in different range returning method names- active,current,selection:
The term "active range" refers to the range that a user has selected in the active sheet, but in a custom function it refers to the cell being "active"ly recalculated.
The current cell is the cell that has focus in the Google Sheets UI, and is highlighted by a dark border.
A selection is the set of cells the user has highlighted in the sheet, which can be non-adjacent ranges. One cell in the selection is the current cell, where the user's current focus is.
The first two are still range objects,while the latter is not. To reiterate, getActiveRange()
in a custom function it refers to the cell being "active"ly recalculated.
I want the function to return the text value A5.
Without Custom Functions,We can use:
=ADDRESS(ROW(),COLUMN())
With Custom function,We can use:
function test() {
return SpreadsheetApp.getActiveRange().getA1Notation();
}

Google Spreadsheet - Insert cut/copied cells/rows

Google Spreadsheet doesn't have the functionality to "insert cut cells" like in Excel.
Let's say I selected A4 to B5, hit Ctrl+X.
Then I moved the selection to A2.
Now I want to "insert cut cells", probably by inserting blank cells and moving the dotted-range to A2.
Is there any way to do it using JavaScript on your own menu?
eg.
function insertCutOrCopiedCells(){
var SS = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SS.getActiveSheet();
var cell = sheet.getActiveCell();
// How do you get the cells being cut/copied? (ie. A4 to B5)
// We can then insert empty cells (2 cols 2 rows) at the selection (A2)
// and move the cut cells (A4 to B5) there
}
To achieve the insert cut cells feature in google sheets you simply use drag and drop. First highlight the row(s) you want to move then mouse over the row number (you will see a hand icon); then, holding your left-click, and keeping your mouse over the row numbers, you will see a dark line where the insert will take place. Let go of your mouse button and voila.
You have both methods, check'em in the class Range.
moveTo(target)
Cut and paste (both format and values) from this range to the target range.
copyTo(destination)
Copies the data from a range of cells to another range of cells. Both the values and formatting are copied.
Edit:
To complete the function you'll have to use also:
insertRowsAfter(afterPosition, howMany) in class Spreadsheet
Inserts a number of rows after the given row position.
and
getActiveRange() with getRow() to check where's the selection at:
Returns the range of cells that is currently considered active. This generally means the range that a user has selected in the active sheet, but in a custom function it refers to the cell being actively recalculated.
Since you don't have direct acess to the clipboard, you'll have to set up a sidebar, or a modelessDialog, which asks for a range to copy from, and it would paste into the selected area, or the other way around, paste the current selected area onto an inputed ROW.
I think , you can separate the function.
Copy : use getRange with getValue:
link => https://developers.google.com/apps-script/reference/spreadsheet/sheet
Delete data : use the getRange with setValue = blank
example :
var Spreadsheet=
SpreadsheetApp.openById(SPREADSHEET_ID).getSheetByName("number1");
var value = Spreadsheet.getRange(1,0).getValue(); // copy
Spreadsheet.getRange(1,1).setValues(value); // insert
Spreadsheet.getRange(1,0).setValues("");
You can use the metho copyTo
example :
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange("A:E").copyTo(sheet.getRange("F1"), {contentsOnly:true});
}