这里有一个建议:
/* let us say source array with name(columnA) & ID(columnB) is array 'source'
and target array with only IDs is array 'target', you get these with something like*/
var source = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getDataRange().getValues();
// and
var target = SpreadsheetApp.getActiveSpreadsheet().getSheets()[1].getDataRange().getValues();// if other columns, change index values in the arrays : 0=A, 1=B ...
// then let's create a 3 rd array that will be the new target with ID + names, and call it 'newtarget'
var newtarget=new Array()
// the iteration could be like this :
for(i=0;i<target.length;++i){ // don't miss any ID
for(j=0;j<source.length;++j){ // iterate through source to find the name
if(target[i][0].toString().match(source[j][1].toString()) == source[j][1].toString()){
var newtargetrow=[source[j][0],target[i][0]] // if match found, store it with name (idx0) and ID (idx 1)
}else{
var newtargetrow=['no name found',target[i][0]] // if no match, show it in name column
}
newtarget.push(newtargetrow);// store result in new array with 2 columns
} //loop source
} // loop target
/* now you have a newtarget array that can directly overwrite the old target
using setValues() */
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheets()[1];// assuming the target sheet is sheet nr2
sh.getRange(1,1,newtarget.length,newtarget[0].length).setValues(newtarget);
//
注意到我没有测试此代码,但它应该给你一个起点。我使用.match比较了字符串,但可以使用其他比较(数组元素之间的直接相等)......如果在工作表数据中存在意外空间的风险,也可以删除ID中的空格...我不'不知道你的数据,所以这取决于你。
有很大的帮助(一如既往!)。 – 2012-07-13 16:51:49
:-)谢谢,我很高兴它有帮助 – 2012-07-13 21:57:17