I use stackoverflow on a regular basis to find answers to my problems, but I cannot seem to solve this one. So here is my first question:
I have a Google form that (amongst other things) asks for the duration of a job. I want the google spreadsheet to contain the form-answers, and add some columns. In this case, I want to add the cost of the job, using an hour rate of 126,-
But I keep running into problems in calculating with the duration: my script either tells me its a text (if I use getDisplayValue in retrieving the data), or it gives me a #NUM error in the spreadsheet itself.
Can anyone pinpoint me towards a solution how to retrieve the hours and the minutes from the form-field (time as duration), so I can do some basic math with it in the script?
I've setup a small form and connected spreadsheet showing my problems. The example form only asks for the duration, and places this in the spreadsheet in column 2. In the spreadsheet I've setup a script that runs on form submit an I try to explain all steps I do. The script should take the form input, convert it to hours (as a num-field) and multiply that with the PricePerHour. The result should be placed in column 3 on the same row of the form submit.
This is my script so far:
// CALCULATE COST OF JOB
function calculatePriceDuration(e) {
// get source data
var sourceSheet = SpreadsheetApp.getActiveSheet(); // connect to source sheet
var sourceRow = sourceSheet.getActiveRange().getRow(); // connect to event row (form submit)
// get destination data
var destinationSheet = sourceSheet; // connect to destination sheet
var destinationRow = sourceRow; // connect to destination row
var destinationColID = 3; // set column number of value to paste
// set variables
var colID_FormDuration = 2; // set column number where the form places the duration
var formDuration = sourceSheet.getRange(sourceRow, colID_FormDuration).getDisplayValue(); // get value for duration
// set price per hour
var PricePerHour = 126;
// calculate job price
var PriceForJob = formDuration * PricePerHour;
// set destination cell
destinationSheet.getRange(destinationRow,destinationColID).setValue(PriceForJob); // paste value in the 3rd column of the same row of form submit
}
the spreadsheet itself can be found here:
the form can be found here:
Any help is much appreciated! Kind regards, Rob