Google Form get Google Sheets Value - google-apps-script

I've got a Google Form to enter deposits and withdrawals into a Google Sheet and then the ending balance is calculated so the user can balance their check book.
I'm trying to get the most recent balance to display on the Google Form by finding the last row with a balance that is in the 'Ending Balance' column.
It seems a script may be easiest but nothing seems to so what I need. Any advice or direction is appreciated.
Here's the sheet I have set up, it's connected to my form with the headers in row 2.
Google Sheet Image

You can
create a bound script attached to your destination spreadsheet
set up a installable trigger onFormSubmit that will fire every time a new form is submitted
access the newest form submission data with getLastRow()
Retrieve the values in column 11 (which contains your Ending balance)
Sample script:
function getMyBalance(){
var sheet=SpreadsheetApp.getActive().getActiveSheet();
var row=sheet.getLastRow();
var column=11;
var balance=sheet.getRange(row, column).getValue();
Logger.log(balance);
}

Related

Get a Complete Score from Spreadsheet

I have a spreadsheet collecting from multiple quizzes. Each varies in length and points available. I am using onFormSubmit trigger in my spreadsheet to copy relevant info (name, email, score) of the most recent submission over to another sheet for the next step of the workflow.
var score = inSheet.getRange(inRow, colNoScore).getValue();
This returns the value of "5", and not "5/10" as it is displayed in the spreadsheet.
How do I access not only the value of correct responses, but also the possible total so that I can create an average?
Instead of getValue() use getDisplayValue()
Reference
https://developers.google.com/apps-script/reference/spreadsheet/range#getdisplayvalue

Google Sheets: how to copy formula down on rows added through Google Form responses

I am working with Google Form results data that is being dropped into a Google Sheet tab and I added a column to calculate the percentage of the quiz scores that are then being pulled onto a tracking tab in the same sheet that calculates their percentage completed. Right now the Percent Column is appearing as a blank field no matter which formula I've tried. I am wondering if there is a different formula would work that would automatically apply to the column when new responses are added? Or would a Google Script be a better option?
I am wanting to keep the raw result data on the same sheet since it is compiling all of the quizzes into one Google Sheet with one tab pulling the Percentages to show completion rate.
I have tried ARRAYFORMULA and the formula that works if you copy it manually to each entry is "=left(C2,find("/",C2)-1)/(right(C2,len(C2)-find("/",C2)))"
Try
=Arrayformula(if(len(C2:C), left(C2:C,find("/",C2:C)-1)/(right(C2:C,len(C2:C)-find("/",C2:C))),))
and see if that works?
This function will add a formula in the last column when a new formresponse will be added to the sheet:
function setFormula() {
var ss=SpreadsheetApp.openById('SHEET_ID');
var sheet=ss.getSheetByName('SHEET_NAME');
var lastRow = sheet.getLastRow();
var formulaRange1 = sheet.getRange(lastRow, sheet.getLastColumn());
formulaRange1.setValue('=IF($A'+lastRow+'="";"";TODAY()-$Q'+lastRow+')');
}
Your formula must be adjusted accordingly. Just make sure that lastRow is inside the string instead of the line number and add a onFormResponse Trigger. I've added this script to the form, not to the spreadsheet.

Trigger for Google Sheets

I have a Google Spreadsheet that will send me an email once a figure in a cell reaches a certain number. I tried setting up triggers, but it doesn't work for what I need.
I need some code to open up the spreadsheet every 2 hours and then check the cell. If the number is greater than what I want, then it emails me.
Does anyone have their own code that I can add to my email function that will do this?
Any help would be appreciated.
The following code will check the value inside a cell and if the number inside the cell is greater than your specified limit, it will send an alert to the given email address.
function sendEmail() {
// get the number in the specified cell. Here I used the cell B2 for reference
var sheet = SpreadsheetApp.getActiveSheet();
var rowNum = sheet.getRange('B2').getValue(); //take the value to rowNum
if(rowNum > your_number_limit ){
var message = 'This is your Alert email!'; // Second column
var subject = 'Your Google Spreadsheet Alert';
MailApp.sendEmail("youremailaddress", subject, message);
}
// Send Alert Email.
}
To check the value every 2 hours do the following
From the Script Editor,
choose Resources > Current project's triggers. You see a panel with the message No triggers set up. Click here to add one now.
Click the link that says No triggers set up. Click here to add one now.
Under Run, select the function you want executed on schedule.
Under Events, select Time-driven.
On the first drop-down list that appears, select Hour timer
set interval of hours for 2 hours.
Click Save.
To ensure that the script runs at the correct time for a particular time zone, click File > Properties, select a time zone and click Save.

How to add date to empty cell by default if none specified in Google Form, on form submit with App Script?

I have a time log form that has a date field I do not want to have to fill out every time as 90% of the time the date would be today's date. (Sometimes you will be entering something from another day and need to change it).
When an entry is added column A is always going to have something, so it should check to see if A is empty and if that same rows D is empty it will input today's date into D.
Some of the example's I have looked at were for onEdit of cell change it, but I could not get it to work with a form submit or even just have it automatically check all the cells in D and if empty put today's date (Only when B has value).
I have a function to sort the sheet right now that is called when on form submit/onEdit happens and would like to stick the new formula in the bottom of the same one (unless that is bad practice). The reason for this is less functions to have to add to the trigger list.
A: Timestamp | B: What | C: Paid? | D: Date
You can try using Date from Google spreadsheets function list which Google Sheets offers.
And, as mentioned in Custom Functions in Google Sheets, if the available functions aren't enough for your needs, you can also use Google Apps Script to write custom functions then use them in Google Sheets just like a built-in function.
To get started, guidelines for custom functions and samples can be found in the given documentations.
You can use Google Apps Script with onFormSubmit trigger which will populate the said cell with default date.
Here is a sample code which will populate the current date in Column 4 of the row in question.
function onFormSubmit(e) {
//edit responses sheet name
var responseSheetName = 'Form Responses 2';
//Edit colmn number, column in which the date has to be autopopulated
var column = 4;
//Get target row number
var row = e.range.rowStart;
//If no date, pouplate the cell with current date
if(!e.values[column-1]){
SpreadsheetApp.getActive().getSheetByName(responseSheetName).getRange(row, column).setValue(new Date())
}
}
In order to setup the above code, open the Spreadsheet which contains the form responses. Go Tools > Script Editor. This will open script editor window. Paste above code and edit the responseSheetName and column . Now Save it.
After saving it, setup an on form submit trigger.
To setup the trigger, follow these steps.
From the script editor, choose Resources > Current project's triggers.
Click the link that says: No triggers set up. Click here to add one now.
Under Run, select the name of function onFormSubmit which you want to trigger.
Under Events, select From spreadsheet.
Select On form submit
Optionally, click Notifications to configure how and when you will be contacted by email if your triggered function fails.
Click Save.

How to catch last update in sheet

I have three Google Forms which submit data to different sheet in Spreadsheet. Google Forms write time stamp in first column when data is submitted. My triggered function send the email notification on submit data to spreadsheet from GForm. I need a different content of notification depending on the sheet of the spreadsheet where data is submitted.
I think about this:
get last update of spreadsheet
switch first sheet to action sheet
find the date equals the last update data in action sheet
if date is not founded, switch to next sheet and find last update date
...
...
if date is founded
get name of action sheet
send content-associated notification
So, think it's too difficult. Of course I can create many spreadsheet with many scripts and each script will send different notification, but it is not ergonomic.
Thank you!
In the event coming from your forms you can get the timeStamp and the row number where the response is recorded. With both criteria I guess it shoudn't be too hard to find the right sheet, just examine this row number in every sheet and compare the time values, if there is a match then you get it !
You can get the values in the eventinfo like this:
Logger.log('row = '+e.range['rowEnd'] + ' timestamp = '+e.namedValues['Timestamp'] )
I didn't test further so please update here is it worked as expected (or not).
Maybe you can add an onEdit trigger:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
var sheet_name = s.getName();
//you can also add a code to get row and col, timestamp, etc.
}
Note: haven't tested this code. I assume this is what you want.