I am working on a data converting script for some elections we are doing. The first part changes all the names to upper case, and this part of the script works fine. However, I have a problem with the second part of the script. Some IDs will have an S, S123456, and some will not have an S, 123456. For my purposes I need all IDs to have no s at the beginning. When I run this script in Google it returns
TypeError: Cannot find function startsWith in object S123456.
Any ideas?
function convertResponseData(){
var resultsInformation = SpreadsheetApp.openById('MySheetID').getSheetByName('Election Responses');
var resultsDataRange = resultsInformation.getRange(1, 1, 2500, 6);
var resultsData = resultsDataRange.getValues();
for (var i = 0; i < resultsData.length; i++) {
var row = resultsData[i];
var resultsStudentNameLower = row[1];
var resultsStudentID = row[2]
var studentNameUpper = resultsStudentNameLower.toUpperCase()
resultsInformation.getRange(i+1, 2).setValue(studentNameUpper)
if (var n = resultsStudentID.startsWith("S")){
var resultsStudentIDNoS = resultsStudentID.substring(1,20);
resultsInformation.getRange(i+1, 3).setValue(resultsStudentIDNoS)
}
}
}
Strict equality (===): a === b results in true if the value a is equal to value b and their types are also the same.
To use CONCAT in Google Sheets, you first need to select the cells or ranges of cells that you want to concatenate. Then, go to the "Formulas" tab and select "CONCATENATE" from the "Text" section. This will open a new dialog box in which you can input the cells or ranges of cells that you want to concatenate.
Select the text or column, then click the Data menu and select Split text to columns... Google Sheets will open a small menu beside your text where you can select to split by comma, space, semicolon, period, or custom character. Select the delimiter your text uses, and Google Sheets will automatically split your text.
New-line characters ("\n") are converted to line-break characters ("\r").
startsWith()
is not supported in google scripts. Instead, you can use
resultsStudentID.indexOf("S") == 0
To check if the string start starts with "S"
function convertResponseData(){
var resultsInformation = SpreadsheetApp.openById('MySheetID').getSheetByName('Election Responses');
var resultsDataRange = resultsInformation.getRange(1, 1, 2500, 6);
var resultsData = resultsDataRange.getValues();
for (var i = 0; i < resultsData.length; i++) {
var row = resultsData[i];
var resultsStudentNameLower = row[1];
var resultsStudentID = row[2]
var studentNameUpper = resultsStudentNameLower.toUpperCase()
resultsInformation.getRange(i+1, 2).setValue(studentNameUpper)
if (resultsStudentID.indexOf("S") == 0 ){
var resultsStudentIDNoS = resultsStudentID.substring(1,20); //You also do this resultsStudentID.substring(1) to get a substring from index 1 to end.
resultsInformation.getRange(i+1, 3).setValue(resultsStudentIDNoS)
}
}
}
Hope that helps
You can now use Modern ECMAScript syntax thanks to V8 Runtime.
You can use startsWith():
function myFunction() {
let str = 'Something';
console.log(str.startsWith("S")); // returns true
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With