I have a script in google Spreadsheet, which gets the active cell and then the row and column of the active cell:
function editedCellResponse() {
var s = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveCell();
var r = s.getRow(); // gets row of edited cell
var c = s.getColumn(); // gets column of edited cell
... script then goes on doing what ever
}
This script also has an onEdit trigger so it's triggered at each edit of the spreadsheet. However either the getActiveCell doesn't work or the getRow and getColumn don't work because the (row, column) result is always (1, 1) regardless of the active cell. The funny thing is as I was first writing the script this all worked and I never changed anything, but now all of a sudden nothing works because the script seems to be reading the active cell or row and column wrong.
Any ideas as to what might be wrong?
Try this:
function onEdit(e){
var r = e.range.getRow();
var c = e.range.getColumn();
Logger.log('%s,%s',r,c);
}
Related
So this is an interesting one...in most simple terms - I have a script that jumps me to a specific cell (based on matches in both the column and row - script below for context):
function Jump(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("In-Office"); // change to sheet containing dates
var r = s.getRange("ah1").getValue(); //change A1 to cell containing =match formula
var row = s.getRange("ai1").getValue();
s.setActiveSelection(s.getRange( r + row ));
}
I have this running from a custom menu, where the user can jump to the cell that contains today's date.
I can use conditional formatting to highlight that cell, but there's been an idea I've wanted to execute in other projects and it feels now would be the time to try it out.
Basically, when the cell jumps, I want the COLOR to change for 10 seconds, then reset itself.
I've tried doing this with macros, but the color change is instant, so you never see it.
I tried implementing a "wait period" similar to how you would while showing a TOAST alert, but wasn't able to figure it out.
My other thought is.. setting up a conditional formatting rule through the script, delaying it for 10 seconds, then deleting it. Seems like a lot to just color the activated cell for a specific amount of seconds.
Anyone ever try something like this before? Essentially, I want to flash a different color for the active cell - I think it could be applicable to many other projects as well.
Here's one way to handle that:
const targetCell = 'B12'; // I'm being lazy here, but hoping you get the idea
const range = s.setActiveSelection(targetCell);
const bgColor = range.getBackground();
range.setBackground('red');
SpreadsheetApp.flush();
Utilities.sleep(10000);
range.setBackground(bgColor);
SpreadsheetApp.flush();
I am adding a new row at the top of an existing ag-grid with this code
function addRow() {
let lastrow = gridOptions.api.getDisplayedRowAtIndex(gridOptions.api.getLastDisplayedRow());
gridOptions.api.applyTransaction({add:[lastrow.data], addIndex:0});
}
this works.
I then want to edit the cells which I do by double clicking on each cell.
Every time I press enter, it calls the grid callback
onCellEditingStopped: function(event) {
const rowNode = gridOptions.api.getRowNode(event.rowIndex);
}
I noticed that when it calls onCellEditingStoppedand tries to get the rowNode it always returns the row that was on top before I added the new row (that initial row is now the second row).
What am I doing wrong?
you can tries the 'onCellValueChanged' instead 'onCellEditingStopped'
I want to write a script that insert a row after the row where cursor is currently located.
For example if my cursor is on "A3" in the spreadsheet, I want to insert a row at 4th row from the script.
I run the following script.
function insertChild(){
var sheet = SpreadsheetApp.getActiveSheet();
var active=sheet.getActiveCell();
sheet.insertRowsAfter(active.getRow(), 1)
}
But this code inserts a row at the second row, instead of fourth row. It seems that getActiveCell() points at "A1" instead of "A3".
Is there any way to insert a row based on the current cursor location?
Your code is totally fine and works.
By the way you could use insertRowAfter(row) if you just want to add one row.
function insertChild(){
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getActiveCell();
sheet.insertRowAfter(cell.getRow());
}
Google Sheets: Replace cell contents with another cell contents when "Clicked" ?
I'm trying to create an easy way to reveal more data from a row when any cell is clicked from the row it lives in. Currently, I have a nice =VLOOKUP("*") ... function that allows me to type in a COMPANY name into a "search bar" cell, and then VLOOKUP will display cell contents depending on which COMPANY it is.
You can see it in action here:
Google Sheets Demo
How To Use:
Green Cells: Type in the "Search Bar" cell for a company name...
Orange Cells: If "COMPANY" is found, it displays the companies cell contents to the left.
What I want to do:
Click on a cell in any row
Have that cell realize what row it's in and navigate to the COMPANY column.
Copy the cells contents from the COMPANY column and replace the current contents of the "search bar" cell
VLOOKUP will then handle the rest.
Possible?
Is this possible with Google Sheet functions alone? Or is this something that should be handled with a script? How would I got about it?
There is currently no way to create an onClick function. The closest thing we have is an onEdit() function. I can think of two ways to make this sort of work.
1) This onEdit version works only if you don't mind locking your script down. You could write an IF statement to create a way to stop this from running temporarily while you edit it.
2) You could setup a second sheet and have the values imported from there or backed up there, and then just run this on the first one at all times.
function onEdit(e) {
//This sets up the worksheet variables
var worksheet = SpreadsheetApp.getActive();
var sheet = worksheet.getActiveSheet();
//This saves the original value e
var origValue = e.oldValue
//This figures out where you clicked, and gets the K+rowyouclicked value.
var selection = sheet.getActiveCell().getA1Notation();
var splitselection = selection.match(/([A-Za-z]+)([0-9]+)/);
var CompName = sheet.getRange("K" + splitselection[2]).getValue();
//set the value on the search bar and return the original value to where you changed.
sheet.getRange("C3").setValue(CompName);
sheet.getActiveCell().setValue(origValue)
}
I have a jgrid with inline editing, when i click on "+" in the pager button it adds a row in editable mode. When the row is in editable mode i click on "+" again at tht time i want to show a message saying "the grid is in edit mode please save it".
document.getElementById('partnerGrid_iladd').onclick = function() {
var rowid = jQuery("#partnerGrid").jqGrid('getGridParam', 'selrow');
var edited = "";
var ind = jQuery("#partnerGrid").getInd(rowid, true);
if (ind != false) {
edited = $(ind).attr("editable");
}
if (edited === "1") {
alert("There is an row in editable mode ,Please save the row before adding another row");
return;
}
}
But this gets fired after a row is added in the grid and the row is in added mode.. So i want to check if the row is a new one if the row is new one i don't want to throw error on click of +.
There are many ways to implement your requirement. You don't posted enough details about what you do, but because you test editable attribute I can guess that you write about the "+" button added by inlineNav. In the case jqGrid calls addRow on the click on the Add button. The method addRow have beforeAddRow callback and jqGridInlineBeforeAddRow event which you can use. The callback/event should return false to prevent adding new row and starting the editing. Moreover you can use savedRow parameter of jqGrid to test whether some other row is in editing mode. You can use getGridParam to get "savedRow" parameter. The returned value is array of saved rows with the values before starting editing. Thus if the length of the array grater as 0 then some other row is still editing.