I have a set of data that is being copy-pasted onto a sheet. The data has a column for duration that is written as X h X min. However, the values are not recognized as number values so the data can't be used for any calculations. I found an onEdit script that will change the text to a proper time value, but it will only work if I edit each cell one by one.
Is there any way to replace this script with one that can be triggered with a button instead of each time I edit the cell?
function onEdit(e) {
var value = e.value;
if (typeof value == 'string') {
var match = value.match(/(\d+) ?h/i);
var hours = match ? match[1] : 0;
match = value.match(/(\d+) ?m/i);
var minutes = match ? match[1] : 0;
match = value.match(/(\d+) ?s/i);
var seconds = match ? match[1] : 0;
if (hours || minutes || seconds) {
var duration = hours/24 + minutes/1440 + seconds/86400;
e.range.setValue(duration).setNumberFormat('[h]"h "m"m "s"s"');
}
}
}
I've tried the following, but it does not work:
function setDuration(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var value = sheet.getRange("C45:C80").getValues();
if (typeof value == 'string') {
var match = value.match(/(\d+) ?h/i);
var hours = match ? match[1] : 0;
match = value.match(/(\d+) ?m/i);
var minutes = match ? match[1] : 0;
match = value.match(/(\d+) ?s/i);
var seconds = match ? match[1] : 0;
if (hours || minutes || seconds) {
var duration = hours/24 + minutes/1440 + seconds/86400;
range.setValues(duration).setNumberFormat('[h]"h "m"m "s"s"');
}
}
}
I'm having trouble wrapping my head around how onEdit scripts work. I understand I need to set up a for loop or an array, but I'm confused on how those work as well.