Im new to Google Sheets Scripts and I was trying to create a code to add Time to cell in an specific column using a button. I got the code down as shown below but I want the .getLastRow function to only apply to one especific column, not to find the very last row in the entire doc.
Any help?
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
function PreOpIN() {
ss.getRange(ss.getLastRow() + 1,9).setValue(new Date())
}
Related
I have a script I am using to copy data from a form in a Google Sheet. ( Yes I know I can just use Google Forms but the client I am working with would like to keep it all in one Google Sheet)
Here is the code I am using now, and it works great. It does exactly what I want, but there is one field that an image is to be placed into. I have it set up so the user goes to "Insert" and then from there inserts an image into the cell. I can manually copy and paste the image from one sheet to another but when I run the script it will not copy over the image all the other data does copy over. Any help would be amazing. Here is the script. The image is in cell I3, as I said everything else works as expected.
function submitData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Form"); //Form Sheet
var datasheet = ss.getSheetByName("Data"); //Data Sheet
//Input Values
var values = [[formSS.getRange("E3").getValue(),
formSS.getRange("I3").getValue(),
formSS.getRange("E6").getValue(),
formSS.getRange("I6").getValue(),
formSS.getRange("E10").getValue(),
formSS.getRange("I10").getValue(),
formSS.getRange("E15").getValue(),
formSS.getRange("I15").getValue(),
formSS.getRange("E19").getValue(),
formSS.getRange("I19").getValue(),
formSS.getRange("E22").getValue(),
formSS.getRange("I22").getValue(),
formSS.getRange("E25").getValue(),
formSS.getRange("I25").getValue(),
formSS.getRange("E28").getValue()]];
datasheet.getRange(datasheet.getLastRow()+1, 1, 1, 15).setValues(values);
formSS.getRange('E3:E29').clearContent();
formSS.getRange('I3:I29').clearContent();
}
I have modified your script to fit the image in the Data sheet.
IMPORTANT: Unmerge the picture cells on the Form sheet and leave the background white. What the script will do is move the values from the Form to the Data (the background color will be moved as well)
The script will get the first value alone and put it in the first column, last row +1.
Now the last row is the one required (not the last +1), and then the script moves the image and inserts the other values as you were doing before.
The script:
function submitData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Form"); //Form Sheet
var datasheet = ss.getSheetByName("Data"); //Data Sheet
//Input Values
var values = [[formSS.getRange("E6").getValue(),
formSS.getRange("I6").getValue(),
formSS.getRange("E10").getValue(),
formSS.getRange("I10").getValue(),
formSS.getRange("E15").getValue(),
formSS.getRange("I15").getValue(),
formSS.getRange("E19").getValue(),
formSS.getRange("I19").getValue(),
formSS.getRange("E22").getValue(),
formSS.getRange("I22").getValue(),
formSS.getRange("E25").getValue(),
formSS.getRange("I25").getValue(),
formSS.getRange("E28").getValue()]];
// Get the first value and insert into the first column
var firstColumn = formSS.getRange("E3").getValue()
datasheet.getRange(datasheet.getLastRow()+1, 1,).setValue(firstColumn);
// Get the image and intert into the second column of the same row
var image = formSS.getRange("I3").moveTo(datasheet.getRange(datasheet.getLastRow(), 2));
// Get all the other values and insert them into the other columns of the same row
datasheet.getRange(datasheet.getLastRow(), 3, 1, 13).setValues(values);
formSS.getRange('E3:E29').clearContent();
formSS.getRange('I3:I29').clearContent();
}
I am trying to create a code to Google Spreadsheets . In VBA code it is very simple. My goal is, when a particular cell is filled , another cell displays the date and time of completion .
In VBA:
Function DateTime()
DateTime = NOW
End Function
Based on your most recent comment stating that you want to call a custom function from the Google Sheet frontend, all you will need in your Script Editor (backend) is:
function timeStamp() {
return new Date();
}
You can then call that function from a formula in your Sheet, very similar to what you wrote already:
=IF( ISBLANK(A1), "", timeStamp() )
This is the code that will help you out of what you are trying to achieve.
Code below will add the date in the very next column of whichever field you will input a value into.
//onEdit is the default "on edit event handler" provided by google app script
function onEdit(e){
var spreadSheet = e.source;
var sheet = spreadSheet.getActiveSheet();
var range = e.range;
//only add the date in the next column if value in cell is not empty
if(range.getValue() != ""){
var col = range.getColumn();
var row = range.getRow();
//get the very next column
var newRange = sheet.getRange(row, col + 1);
newRange.setValue(new Date());
}
}
Hope this will help.
NB: Please note this code will work with scripts bound to google apps only.
background
I followed this tutorial which basically makes a button call a google sheets script like this
function jumpToDetail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("details");
ss.setActiveSheet(sheet).setActiveSelection("A2");
}
that makes you jump to a specific cell. I looked at the google sheets docs but couldn't figure out how to make this function reference the cell it was called from
question
How do I make a function in google sheets reference the function it was called from. For example I would like the final code to look something like this:
function jumpToDetail(clickedCell) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("details");
// get the value of "id" of the same row the cell was clicked in
ss.setActiveSheet(sheet).setActiveSelection(function_of(id));
}
The active range is obtained with getActiveRange method of SpreadsheetApp. This can be useful when a script is called from a custom function: the active range is then the cell at which the custom function is located. Example:
function currentCell() {
return SpreadsheetApp.getActiveRange().getA1Notation();
}
Entering =currentcell() anywhere in the sheet will return the A1 notation of that cell.
But clicking a button (an inserted drawing) has no effect on active range, because drawings are not associated with any cell in a sheet. They are in their own layer, floating over the sheet. In a script called with a button, the active range is wherever the selected cell(s) happen to be, and has nothing to do with the location of the button.
In order to communicate some location to a script, the user would have to select a cell first and then call a script (via a button or a menu).
Here is a function that returns the content of the cell in the current row with column labeled "id". (The label is assumed to be in the 1st row of the sheet.)
function getId() {
var row = SpreadsheetApp.getActiveRange().getRow();
var sheet = SpreadsheetApp.getActiveSheet();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var column = headers[0].indexOf('id') + 1;
if (column > 0) {
return sheet.getRange(row, column).getValue();
}
else {
throw new Error('No column labeled id');
}
}
I am currently trying to write a script on Google apps script for Google Sheets that will insert a row directly underneath the current active cell. I also want to copy down the contents of only Col B.
Here is what I have thus far:
This snip-bit works great, but there is no "active cell" feature
function addrow5() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[1];
// This inserts a row after the first row position
sheet.insertRowAfter(5);
So, I've researched and found found this:
sheet.insertRowAfter(activeCell.getRow());
but I'm not exactly how to put it all together. As far as the copying of Col B, I've yet to chart those waters.
Thank you for any help!
I probably answered this one already on the google docs Help forum but in case somenone else is interested:
function addrow() {
var sheet = SpreadsheetApp.getActiveSheet(),
r = sheet.getActiveCell()
.getRow(),
bVal = sheet.getRange(r, 2).getValue();
sheet.insertRowAfter(r);
sheet.getRange(r + 1, 2).setValue(bVal);
}
I'm using the new Google Sheets API and any every time I try to get the active cell, it just returns cell A1 in the first sheet, no matter what. Here's my code:
var s = SpreadsheetApp.getActiveSpreadsheet();
var ss = s.getActiveSheet();
var result = ss.getActiveCell().getValue();
Seems like a Google bug...
This is currently working perfectly for me. If I highlight a cell in the active sheet and run your code, the value in the cell is logged for me.
You must use getActiveSheet() to specify the sheet. You cannot specify the sheet with getSheetByName()