Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String starts with in Google Script

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)
    }
  }
}
like image 802
Austin Hoag Avatar asked Apr 05 '17 00:04

Austin Hoag


People also ask

What is === in Google script?

Strict equality (===): a === b results in true if the value a is equal to value b and their types are also the same.

How do I concatenate in a Google script?

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.

How do I split a string in Google script?

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.

How do you add a line in an app script?

New-line characters ("\n") are converted to line-break characters ("\r").


2 Answers

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

like image 163
Jack Brown Avatar answered Oct 18 '22 13:10

Jack Brown


Update 2020:

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
}
like image 43
soMarios Avatar answered Oct 18 '22 11:10

soMarios