我仍然是相当新的Apps脚本,我正在尝试执行以下操作:
循环播放Sheet1 C列,其中包含user_names。我想将Sheet1列C中的user_names列表与Sheet 2列A中包含的列表进行比较。如果Sheet 1 C列中存在usernames,而Sheet 2列A中不存在,我想将它们复制到工作表2列A中列表的结束。如果两个列表中都存在user_names,我不想复制它们。
我希望Sheet 2列A中user_names的顺序保持不变,无论Sheet1 Column C如何更改。我希望Sheet1 Column C中列表中的任何新增内容都复制到Sheet 2 A列中列表的 end ,即使在中间<中找到了Sheet1 C列的新增内容/ strong>该列表(见下面的例子)。
Sheet1 C列中user_names的顺序每天都在变化。
Examples:
Given:
Sheet1 Column C | Sheet 2 Column A
-----------------------------------
username1 |
username2 |
username3 |
I want:
Sheet1 Column C | Sheet 2 Column A
-----------------------------------
username1 | username1
username2 | username2
username3 | username3
The next day, when the usernames update:
Given:
Sheet1 Column C | Sheet 2 Column A
-----------------------------------
username1 | username1
username3 | username2
username5 | username3
username4 |
username2 |
I want:
Sheet1 Column C | Sheet 2 Column A
-----------------------------------
username1 | username1
username3 | username2
username5 | username3
username4 | username5
username2 | username4
(在最后一个示例中,username4和用户名5可以按任意顺序排列)
这是一个功能的草稿 - 我玩了很多版本,但没有一个工作。我使用this question作为参考。
function copy_new_names() {
var spreadsheet = SpreadsheetApp.openById('MY ID IS HERE -- THIS IS A PLACEHOLDER');
var sheet = spreadsheet.getSheetByName('Sheet1');
var sheet2 = spreadsheet.getSheetByName('Sheet2');
var startingrow = 2;
var endrow = sheet.getLastRow();
var endcol = sheet.getLastColumn();
var endrow2 = sheet2.getLastRow();
var endcol2 = sheet2.getLastColumn();
var data = sheet.getDataRange().getValues();
var data2 = sheet2.getDataRange().getValues();
var resultArray = [];
for (var n in data){
var keep = true
for (var p in data2){
if( data[n][2] == data2[p][0]){
keep=false ; break ;
}
}
Logger.log(keep);
if(keep){ resultArray.push(data[n])};
sheet2.getRange(endrow2 + 1, 1,resultArray.length,resultArray[0].length).setValues(resultArray);
Logger.log(resultArray);
}
}
任何帮助都会很棒!!非常感谢!
答案 0 :(得分:1)
此代码可以满足您的需求。我测试了它,它的确有效:
function copy_new_names() {
var spreadsheet, sheet, sheet2, endrow, endcol,endrow2,endcol2,data, data2,resultArray,n,l,l2,p,doesNotExist,
sheetOneValue, numberOfMissingValues, innerArray,i,thisValue;
spreadsheet = SpreadsheetApp.openById('');
sheet = spreadsheet.getSheetByName('Sheet1');
sheet2 = spreadsheet.getSheetByName('Sheet2');
endrow = sheet.getLastRow();
endcol = sheet.getLastColumn();
endrow2 = sheet2.getLastRow();
endcol2 = sheet2.getLastColumn();
data = sheet.getRange(1, 3, endrow, 1).getValues();
//getRange(start row, start column, number of Rows, number of Columns)
data2 = sheet2.getRange(1, 1, endrow2, 1).getValues();
data2 = data2.toString().split(","); //Flatten the 2D array to one D
resultArray = [];
innerArray = [];
l = data.length;
l2 = data2.length;
for (n=0;n<l;n+=1) {
sheetOneValue = data[n][0];
innerArray = []; //Reset
for (p=0;p<l2;p+=1) {
doesNotExist = data2.indexOf(sheetOneValue) === -1;//If the value is NOT found, indexOf returns -1
if (!doesNotExist) break; //If it exists in the list, no need to go any further
if (doesNotExist) {//If the value in the cell from data one does NOT exist in data two, add it to the array
innerArray.push(sheetOneValue)
resultArray.push(innerArray);
Logger.log('sheetOneValue: ' + sheetOneValue);
break ;
};
};
};
endrow2 = 0;
for (i=0;i<l;i+=1) {
thisValue = data2[i];
if (thisValue==="" || thisValue=== undefined) {//There is nothing in this cell
continue;
};
endrow2+=1;
};
//Append the data in the array to the bottom of the data two list
sheet2.getRange(endrow2+1, 1,resultArray.length,1).setValues(resultArray);
Logger.log(resultArray);
};
答案 1 :(得分:1)
我找到了一个有点不同的解决方案,但仍然有效...谁能最少;) 如果你想测试......
// You can activate a trigger on function copy_new_names()
// Assuming that row 1 in each sheet is not included in the values to be processed...
function copy_new_names() {
var spreadsheet = SpreadsheetApp.openById('spreadsheet_id');
var sheet = spreadsheet.getSheetByName('Sheet1');
var sheet2 = spreadsheet.getSheetByName('Sheet2');
var endrow = sheet.getLastRow(), endcol = sheet.getLastColumn();
var endrow2 = sheet2.getLastRow(), endcol2 = sheet2.getLastColumn();
// assuming that row 1 is not included in the values to be processed
var valuesSheet1 = sheet.getRange(2, 3, endrow-1, 1).getValues(), valuesSheet2 = [];
if ( endrow2 > 1 ) valuesSheet2 = sheet2.getRange(2, 1, endrow2-1, 1).getValues();
var newlist = checkList(valuesSheet1, valuesSheet2);
//Below to paste new list in sheet2
sheet2.getRange(2, 1, newlist.length, 1).setValues(newlist);
}
function checkList(valuesSheet1, valuesSheet2) {
var values1 = valuesSheet1.map(function(d) { return d.join()}), values2 = [];
if ( valuesSheet2.length ) values2 = valuesSheet2.map(function(d) { return d.join()});
var newList = [];
for ( var i in values1 ) {
if ( values2.indexOf(values1[i]) === -1 ) newList.push([values1[i]]);
}
newList = valuesSheet2.concat(newList);
//Logger.log(newList)
return newList;
}