The line starts with Google Script

I am working on a data conversion script for some of the choices we make. The first part changes all the names to uppercase, and this part of the script works fine. However, I have a problem with the second part of the script. Some identifiers will have S, S123456, and some will not have S, 123456. For my purposes, I need all of the identifiers to have no s in the beginning. When I run this script on Google, it returns "TypeError: Cannot find startWith function in object S123456". Any ideas? Thanks!!

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) } } } 
+5
source share
1 answer

startsWith() not supported in google scripts. You can use instead

 resultsStudentID.indexOf("S") == 0 

To check if the beginning of a line begins 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

+7
source

Source: https://habr.com/ru/post/1266333/


All Articles