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());
}
Related
I just started exploring google sheets and javascript and I wrote a function which prompts the user to type a string which is entered into the selected cell. I'm accessing the current cell using SpreadsheetApp.getActiveSheet().getActiveCell() and setting the value using .setValue().
Now I made another prompt and I want to put this value in the cell next to the selected cell (same row but column+1). How do I access the current cells row and column positions and then increment the col with 1?
In pseudo code im thinking something like this;
ActiveCell[row][column+1] = string input from prompt2
Try using offset to specify offset:
let cell = SpreadsheetApp.getActiveSheet().getActiveCell();
// update cell to the right
cell.offset(0, 1).setValue("upd");
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 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);
}
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)
}
Wasn't really sure how to ask the question, but my situation is this...
I have a table with one row, which contains a few input boxes. When a user presses the add new, I have managed to add another row to the table. First I make a new variable called oldtable, and then split it at each <tr>.. and save that to the array rows. I use this code:
var newdata="";
for(i=0;i<rows.length;i++)
{
// adds the next row on...
newdata=newdata+rows[i]+"<tr>";
}
newdata=newdata+"MY NEW ROW GOES HERE!";
Then I go on to reset the table innerHTML to the newdata variable.
All works fine, until the user enters data in the first row, and then goes to add data in the second. Then the values on the first row are gone, understandably. Taking into account that the table can theoretically have as many rows as the user wants, how can I go about keeping the values of the first row intact? Is there a way to insert data into the table without reseting what's there?
Thanks.
var olddata = document.getElementById("products_table").innerHTML;
// This splits up each row of it
var rows = olddata.split("<tr>");
// Makes a variable where the new data is going to be stored
var newdata = "";
// This loop is to add back the existing html into the table, it is one short, because we Omit the "add new product" row.
for(i=0;i<rows.length-1;i++)
{
// adds the next row on...
newdata=newdata+rows[i]+"<tr>";
}
// Adds the additional html needed to
newdata=newdata+"my new row goes here!";
// Add newly edited table back in...
document.getElementById("products_table").innerHTML=newdata;
Instead of manipulating HTML in string form (very error prone), you should use the built-in DOM manipulation methods to add new rows to your table. Make sure your table has a TBODY element and give it an ID:
<table id = "product_table">
<tbody id = "product_table_tbody">
<tr>
<td>A Cell</td>
</tr>
</tbody>
</table>
Then, attach an event handler to the Add Row button and do something like this:
function addRow() {
var myTbody = document.getElementById("products_table_tbody");
var myRow = document.createElement("tr");
var myCell = document.createElement("td");
myCell.innerHTML = "My new cell";
myTbody.appendChild(myRow);
myRow.appendChild(myCell);
}