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.
Related
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())
}
I'm looking for a script for google sheet that automatically inserts a timestamp for each row. and the date and time will be updated every time any of the rows gets edited. Timestamp will be put on column 1 and the rows can be the whole rows after the row 1, no specific number of rows. Please help I am very new at this. Need it badly. Thank you
You can use an onEdit trigger from Google Apps Script.
To do that, open a script bound to your spreadsheet by clicking Tools > Script editor, copy the following code and save the project:
function onEdit(e) {
var row = e.range.getRow();
if (row > 1) { // Check edited row is not first one
var formattedDate = Utilities.formatDate(new Date(), e.source.getSpreadsheetTimeZone(), "yyyy-MM-dd'T'HH:mm:ss'Z'"); // Format you want the timestamp, edit accordingly
e.range.getSheet().getRange(row, 1).setValue(formattedDate);
}
}
Reference:
Date
onEdit
Utilities.formatDate(date, timeZone, format)
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've been busy trying to use the build-in javascript in Google Spreadsheet, however, not having worked in either javascript or Google Spreadsheet, i'm having a few difficulties.
My script is supposed to read a number (1-3) in a cell, and from that number parse an image to the cell below (I've been using the setFormula command for this).
So far it's working for 1 cell (B6 as i've choosen right now), but i would like to loop through a column with numbers in every other cell (So that after the script has run, it's number-picture-number-picture etc) - i just can't figure out how.
The code i'm using right now:
function numbtoimage() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var url = 'IMAGE("https://dl.dropboxusercontent.com/s/bpqy8o796casqjl/belt.JPG?dl=0", 2)';
var url2 = 'IMAGE("https://dl.dropboxusercontent.com/s/4q8sakhkpot0h65/belt2.JPG?dl=0",2)';
var url3 = 'IMAGE("https://dl.dropboxusercontent.com/s/kvsf4z6z45rcg53/belt3.JPG?dl=0",2)';
var cell = sheet.getRange("B6")
var data = cell.getValue()
if(data==1) {cell.offset(1, 0, 1).setFormula(url);}
else if(data==2) {cell.offset(1, 0, 1).setFormula(url2);}
else if(data==3) {cell.offset(1, 0, 1).setFormula(url3);}
}
I've looked at This similar problem, but have been unable to make it work for my case.
Any help is greatly and truly appreciated!
Nicklas
You need some sort of loop to go through the data. I Would suggest a FOR loop.
Your script is currently written to get one single cell value, rather than all the values.
So it might be an idea to get all values in one go, then check whats in them.
Also from you question, it's not clear where the numbers will be found.
Only in column B?
Here is a quick example (untested), that goes through column B looking for a number and it should insert the link in the cell below based on that number. This code is based on your original example and untested but hopefully it helps.
function numbtoimage() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var url = 'IMAGE("https://dl.dropboxusercontent.com/s/bpqy8o796casqjl/belt.JPG?dl=0", 2)';
var url2 = 'IMAGE("https://dl.dropboxusercontent.com/s/4q8sakhkpot0h65/belt2.JPG?dl=0",2)';
var url3 = 'IMAGE("https://dl.dropboxusercontent.com/s/kvsf4z6z45rcg53/belt3.JPG?dl=0",2)';
var values = sheet.getValues();
for(i=0; i < values.lenth ; i++){
if(values[i][1]==1) {sheet.getRange(i+2, 2).setFormula(url);}
else if(values[i][1]==2) {sheet.getRange(i+2, 2).setFormula(url2);}
else if(values[i][1]==3) {sheet.getRange(i+2, 2).setFormula(url3);}
}
}
I'm trying to make a script that sends out a custom email confirmation to the user. However, I noticed that if any of the cells are blank the script will not work. I was wondering how to I like highlight the last entry of the table and check the cells, that if the cell is empty then change it's value to 0 or a dash. Here's what I got going:
function Confirmation(e) {
var sheet = SpreadsheetApp.getActiveSheet();
var lastRow = sheet.getLastRow();
var lastRange = sheet.setActiveRange(sheet.getRange(lastRow,5,1,2));
//lastRange.setValues(1111);
var EmailAdd = e.values[1];
var Name = e.values[2];
var Type = e.values[3];
var Criteria1 = e.values[4];
var Criteria2 = e.values[5];
var Criteria3 = e.values[5];
MailApp.sendEmail(EmailAdd, "Your request has been received" ,
"Thank you, " + Name + " for submitting your request.
)
}
the
lastRange
line already highlights the last entry. How can I evaluate each cell and check if it's empty change it's value and if not move to the next cell.
*I always get an "cannot read property "values" from undefined" is this because it's empty or because I set it as an event triggered function. Sorry I'm new to javascript & google apps script
As indicated in the comment above, Mogsdad's link provides a good work around for this problem. See link: e.values in google forms skips empty answers, is there a workaround?