这是我第一次尝试使用Google表格脚本
我尝试比较两页中第一列的每一行。
如果工作表1中的任何值不在工作表2中,我将其添加到工作表2的末尾。
这是代码,比较值永远不匹配,我尝试了==,===,有和没有toSting()。我从没得到found=1
标志,sheet1 col1中的所有值都复制到sheet2 col1。
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
function Prueba2() {
var sheet = ss.getSheets()[0];
var rangeData = sheet.getDataRange();
sheet.getRange('A1').activate();
var lastRow = rangeData.getLastRow();
var searchRange = sheet.getRange(2,1, lastRow, 1);
var sheet2 = ss.getSheets()[1];
var rangeData2 = sheet2.getDataRange();
var lastRow2 = rangeData2.getLastRow();
var searchRange2 = sheet2.getRange(2,1, lastRow2, 1);
var l1= searchRange.getLastRow();
var l2= searchRange2.getLastRow();
var rangeValues = searchRange.getValues();
var rangeValues2 = searchRange2.getValues();
var found=0;
var arr = [];
// Loop through array and if condition met
for ( i = 0; i < lastRow - 1; i++){
found=0;
for ( j = 0 ; j < lastRow2 - 1; j++){
var vi=rangeValues[i];
var vj=rangeValues2[j];
if(rangeValues[i].toString() === rangeValues2[j].toString()){
found=1;
break;
};
};
if (found==0){
Logger.log(rangeValues[i]);
arr.push(rangeValues[i]);
};
};
var toAddArray = [];
for (k = 0; k < arr.length; ++k){
toAddArray.push([arr[k]]);
}
sheet2.getRange(lastRow2+1, 1, arr.length, 1).setValues(toAddArray);
};
编辑
这行得通,但我不知道为什么
for ( j = 0 ; j < lastRow2 - 1; j++){
var vi=rangeValues[i].toString();
var vj=rangeValues2[j].toString();
//if(rangeValues[i] === rangeValues2[j]){
if(vi===vj||vi==""){
found=1;
break;
};
};
答案 0 :(得分:2)
此修改如何? getValues()
检索的值是二维数组。通过考虑这一点,可以修改脚本。
如果您的脚本已修改,那么如何修改?
for ( i = 0; i < lastRow - 1; i++){
found=0;
for ( j = 0 ; j < lastRow2 - 1; j++){
var vi=rangeValues[i];
var vj=rangeValues2[j];
if(rangeValues[i].toString() === rangeValues2[j].toString()){
found=1;
break;
};
};
if (found==0){
Logger.log(rangeValues[i]);
arr.push(rangeValues[i]);
};
};
for ( i = 0; i < lastRow - 1; i++){
found=0;
for ( j = 0 ; j < lastRow2 - 1; j++){
// var vi=rangeValues[i]; // This is not used in this script.
// var vj=rangeValues2[j]; // This is not used in this script.
if(rangeValues[i][0].toString() === rangeValues2[j][0].toString()){ // Modified
found=1;
break;
};
};
if (found==1){ // Modified
Logger.log(rangeValues[i][0]); // Modified
arr.push(rangeValues[i][0]); // Modified
};
};
if(rangeValues[i][0].toString() === rangeValues2[j][0].toString()){
替换为if(rangeValues[i][0] === rangeValues2[j][0]){
。与其他模式一样,此修改如何?
var found=0;
var arr = [];
// Loop through array and if condition met
for ( i = 0; i < lastRow - 1; i++){
found=0;
for ( j = 0 ; j < lastRow2 - 1; j++){
var vi=rangeValues[i];
var vj=rangeValues2[j];
if(rangeValues[i].toString() === rangeValues2[j].toString()){
found=1;
break;
};
};
if (found==0){
Logger.log(rangeValues[i]);
arr.push(rangeValues[i]);
};
};
var toAddArray = [];
for (k = 0; k < arr.length; ++k){
toAddArray.push([arr[k]]);
}
sheet2.getRange(lastRow2+1, 1, arr.length, 1).setValues(toAddArray);
var toAddArray = rangeValues.filter(function(e) {return rangeValues2.some(function(f) {return e[0] && f[0] && e[0] == f[0]})});
sheet2.getRange(lastRow2+1, 1, toAddArray.length, 1).setValues(toAddArray);
vi===vj
起作用的原因如下。
rangeValues[i].toString()
和rangeValues2[j].toString()
时,一维数组将转换为类似["sample"]
到"sample"
的字符串。这样vi===vj
起作用了。如果我误解了您的问题,而这不是您想要的结果,我深表歉意。