如何在Google Apps脚本中比较字符串 [英] How to compare strings in google apps script
本文介绍了如何在Google Apps脚本中比较字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试比较从Google电子表格中获得的字符串值.
I am trying to compare string values that I have obtained from my google spreadsheet.
我的月份以"Jun13","Aug13"等格式显示,并且我的脚本在比较这些格式的值时遇到麻烦
My Months appear in this format " Jun13", "Aug13" etc. and my script has trouble comparing the values when they are in such formats
var data = ss.getRange(2,2,ss.getLastRow(),3).getValues();
var ListOfMonths = new Array();
for(var i = 0; i < data.length; ++i){
var row = data[i][0];
var duplicate = false;
for(j in ListOfMonths){
if(row.toString.match(ListOfMonths[j][0])){
duplicate = true;
}
if(!duplicate){
ListOfMonths.push(row);
}
}
}
有人可以帮我解决比较字符串的问题吗?我也尝试过==和===,但是它们都不起作用
Could someone help me with this issue of comparing strings? I also tried == and === but they all don't work
推荐答案
尝试:
function test(){
var ss = SpreadsheetApp.getActive().getSheetByName("month test");
var data = ss.getRange(2,2,ss.getLastRow(),3).getValues();
var ListOfMonths = new Array();
for(var i in data){
var row = data[i][0].toString();
if(ListOfMonths.indexOf(row)==-1){
Logger.log(row+" is not referenced. Adding it");
ListOfMonths.push(row);
}
}
Logger.log(ListOfMonths);
}
这篇关于如何在Google Apps脚本中比较字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文