I am new to coding with google apps script and am trying to make a simple case checker for my spreadsheet.
In this case, I want to check if the first letter of a name is capitalized, and if it isn't, I want to replace it with a corrected version (same name, just with the first letter capitalized.)
This code returns an error when trying to run it in the editor (cannot read property "range" of undefined.) I presume this is because it is using an onFormSubmit trigger, so the only way to test it would be to actually submit a form response.
When a new form response IS posted and the function is triggered, it doesn't seem to do anything on the spreadsheet's end. The new cells that contain the first name and last name (or any of the new cells for that matter) simply don't change, first-letter-capitalized or not.
When using a separate function that refers to the cells the same way (taking a range of the newly submitted cells and designating them row[numberhere] within the for loop) it reads their strings just fine.
function caseCheck(r) {
var range = r.range;
var data1 = range.getValue();
for (var r in data1) {
var row1 = data1[r];
var firstName = row1[2].getValue;
var lastName = row1[3].getValue;
var firstNameC = firstName[0].toUpperCase();
var lastNameC = lastName[0].toUpperCase();
if (firstName[0] != firstNameC) {
var firstNameL = firstName.length();
var firstNameSS = firstName.substring(1,firstNameL);
var firstNameCorrected = firstNameC + firstNameSS;
row1[2].setValue(firstNameCorrected);
}
if (lastName[0] != lastNameC) {
var lastNameL = lastName.length();
var lastNameSS = lastName.substring(1,lastNameL);
var lastNameCorrected = lastNameC + lastNameSS;
row1[3].setValue(lastNameCorrected);
}
}
}