I have the following code, I want the first if statement to be true only where r[18] < 6 and for the cells in column 14 which start with "C" I'm trying to use a regular expression to extract the first letter but unfamiliar using Regex. Can someone please assist. Thanks
const ss = SpreadsheetApp.openById('10D0Qa8Y6nrAgeuS0V6-GyKkQBRooMUwseRc8agi7iN4');
const sh = ss.getSheetByName("QUOTATIONS");
const vs = sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues();
const prod = sh.getRange(2, 14, sh.getLastRow() - 1, 1).getValues();
RegExp(/^C/i,prod,0);
vs.forEach((r,i) => { if(r[18] < 6 && RegExp(/^C/i,prod,0))
sh.getRange(i 2,33).setNumberFormat("0.#0").setValue((r[18] * r[19]) (r[21] * r[22]) (r[24] * r[25]) (r[27] * r[28]) (r[28] * r[31]) (6 - r[18]) * 20)
vs.forEach((r,i) => { if(r[18] >= 6)
sh.getRange(i 2,33).setNumberFormat("0.#0").setValue((r[18] * r[19]) (r[21] * r[22]) (r[24] * r[25]) (r[27] * r[28]) (r[28] * r[31]))
});
}
)}```
CodePudding user response:
If you need just a first letter you don't even need RegExp. It can be done with slice()
this way:
var s = 'abc'
console.log(s.slice(0,1)) // output: 'a'
Or even:
var s = 'abc'
console.log(s[0]) // output: 'a'
If you want RegExp it can be string.match(/reg/)
or /reg/.test(string)
:
var s = 'abc'
if (s.match(/^a/i)) { console.log('yes') } else { console.log('no') } // yes
if (s.match(/^b/i)) { console.log('yes') } else { console.log('no') } // no
if (/^a/i.test(s)) { console.log('yes') } else { console.log('no') } // yes
if (/^b/i.test(s)) { console.log('yes') } else { console.log('no') } // no