字符串以Google脚本开头 [英] String starts with in Google Script

查看:71
本文介绍了字符串以Google脚本开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我们正在进行的某些选举制作数据转换脚本.第一部分将所有名称更改为大写,脚本的这一部分工作正常.但是,我对脚本的第二部分有疑问.有些ID带有S,S123456,有些则没有S,123456.出于我的目的,我需要所有ID开头都没有s.当我在Google中运行此脚本时,它会返回

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:在对象S123456中找不到函数startsWith.

TypeError: Cannot find function startsWith in object S123456.

有什么想法吗?

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)
    }
  }
}

Google脚本不支持

推荐答案

startsWith().相反,您可以使用

resultsStudentID.indexOf("S") == 0 

要检查字符串是否以"S"开头

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)
    }
  }
}

希望有帮助

这篇关于字符串以Google脚本开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆