I wrote some working code in spreadsheet scripts, but it's a little bulky and slow. Is there any way to refactor this to be more compact and faster?
I'm a beginner, so I'm happy it works at the moment. So no worries if you can't help or it involves too long to give a solution. I would love to learn.
function extract() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getActiveSheet();
var dt = ss.getDataRange().getValues();
var res = new Array();
for (var i = 1; i < dt.length; i++) {
if (dt[i][3].toString().charAt(0) == "E") {
res.push([dt[i][5], dt[i][6]])
} else if ((dt[i][5] != null && dt[i][5] != "") || (dt[i][6] != null && dt[i][6] != "")) {
res.push([dt[i][5], dt[i][6]])
} else if (dt[i][3].toString().trim() != null && dt[i][3].toString().trim() != "") {
if (dt[i][2] == "DMM" || dt[i][2] == "Rock Exotica" || dt[i][2] == "Lyon") {
var tt = methd1(dt[i][3])
} else if (dt[i][2] == "Petzl" || dt[i][2] == "Marlow") {
var tt = methd2(dt[i][3])
} else if (dt[i][2] == "CMC") {
var tt = methd3(dt[i][3])
} else if (dt[i][2] == "Singing Rock") {
var tt = methd4(dt[i][3])
} else if (dt[i][2] == "Taz") {
var tt = methd5(dt[i][3])
} else {
tt = [null, null]
}
res.push(tt)
} else {
res.push([null, null])
}
}
sh.getRange(2, 6, res.length, res[0].length).setValues(res)
}
function methd5(vl) {
var dte = new Date();
var yr;
var mnth;
mtch = vl.toString().match(/(?<=^...)\d{4}/gm)
if (mtch != null) {
mtch = mtch[0]
yr = parseInt(mtch.substring(0, 2))
if (yr <= (dte).getFullYear() - 2000) {
yr = yr + 2000;
} else {
yr = yr + 1900;
}
mnth = parseInt(mtch.substring(2, ))
}
return ([yr, mnth])
}
function methd4(vl) {
var dte = new Date();
var yr;
var mnth;
mtch = vl.toString().match(/\d{4}$/gm)
if (mtch != null) {
mtch = mtch[0]
yr = parseInt(mtch.substring(2, ))
if (yr <= (dte).getFullYear() - 2000) {
yr = yr + 2000;
} else {
yr = yr + 1900;
}
mnth = parseInt(mtch.substring(0, 2))
}
return ([yr, mnth])
}
function methd3(vl) {
var dte = new Date();
var yr;
var mnth;
mtch = vl.toString().match(/^\d\d\d\d\d\d\d/gm)
if (mtch != null) {
mtch = mtch[0]
yr = parseInt(mtch.toString().substring(0, 4))
mnth = (new Date((new Date(dte.getFullYear(), 0, 1)).getTime() + parseInt((mtch.substring(2, ))) * 24 * 60 * 60 * 1000)).getMonth() + 1
}
return ([yr, mnth])
}
function methd2(vl) {
var dte = new Date();
var yr;
var mnth;
var rr = [null, null]
var lst = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"]
var mtch = vl.toString().match(/^\d\d[A-Z]/gm)
if (mtch != null) {
mtch = mtch[0]
yr = parseInt(mtch.substring(0, 2))
if (yr <= (dte).getFullYear() - 2000) {
yr = yr + 2000;
} else {
yr = yr + 1900;
}
mnth = lst.indexOf(mtch.substring(2, )) + 1
rr = [yr, mnth]
} else {
mtch = vl.match(/^\d\d\d\d\d/gm)
if (mtch != null) {
mtch = mtch[0]
yr = parseInt(mtch.toString().substring(0, 2))
if (yr <= (dte).getFullYear() - 2000) {
yr = yr + 2000;
} else {
yr = yr + 1900;
}
mnth = (new Date(((new Date(dte.getFullYear(), 0, 1)).getTime() + parseInt((mtch.substring(2, ))) * 24 * 60 * 60 * 1000))).getMonth() + 1
rr = [yr, mnth]
}
}
return (rr)
}
function methd1(vl) {
var dte = new Date();
var yr;
var mnth;
mtch = vl.match(/^\d\d\d\d\d/gm)
if (mtch != null) {
mtch = mtch[0]
yr = parseInt(mtch.toString().substring(0, 2))
if (yr <= (dte).getFullYear() - 2000) {
yr = yr + 2000;
} else {
yr = yr + 1900;
}
mnth = (new Date((new Date(dte.getFullYear(), 0, 1)).getTime() + parseInt((mtch.substring(2, ))) * 24 * 60 * 60 * 1000)).getMonth() + 1
}
return ([yr, mnth])
}