我仍然在谷歌电子表格上进行组合业务,我有两列,A和B,代表货币及其代码,我想要两种方式的所有“转换”组合。
我成功编写了代码,但是,我现在想要消除重复:我的意思是,在结果中,我将“将美元兑换成欧元”,“将欧元兑换成美元”,“将美元兑换成欧元” ,“欧元兑美元”,“欧元兑美元汇率”和“美元兑欧元汇率”。
但是,我也会有“欧元兑欧元”。
我如何在我的代码中解决这个问题:
function matrix() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = 'Sheet1!B4:C19';
var destID = '1kVhuTwVr80AScne9ijtlWs9YlDf5YkixIFVVbPjoX5E';
var destcell = 'Sheet1!D27';
var curr = SpreadsheetApp.getActiveSpreadsheet().getRange(range).getValues();
var currConv = [];
for (var i = 0; i < curr.length; i++)
{
for ( var j = 0; j < curr.length; j++)
{
currConv.push(['Convert ' + curr[i][0] + ' to ' + ' ' + curr[j][0]]);
currConv.push(['Convert ' + curr[i][0] + ' to ' + ' ' + curr[j][1]]);
currConv.push(['Convert ' + curr[i][1] + ' to ' + ' ' + curr[j][0]]);
currConv.push(['Convert ' + curr[i][1] + ' to ' + ' ' + curr[j][1]]);
}
}
var destRange = SpreadsheetApp.openById(destID).getRange(destcell).offset(0, 0, currConv.length);
destRange.setValues(currConv);
}
我试图插入类似“i!= j”的内容,但它给了我一个错误。
非常感谢你的帮助!
答案 0 :(得分:0)
首先,您需要找到所有重复项的索引位置。有一些stackoverflow答案可以在数组中查找重复项,如下面的链接:
stackoverflow - answer - find duplicates
我在上面的链接中修改了这个案例的代码。
function removeDuplicates(arr) {
//Code adapted from SO https://stackoverflow.com/a/840849/2946873
arr = [9, 9, 9, 111, 2, 3, 3, 3, 4, 4, 5, 7];
var i,
len=arr.length,
out=[],
obj={},
hasItAlreadyBeenFound="",
indexPositionsToDelete=[];
for (i=0;i<len;i++) {
//check if this value is already in the object
hasItAlreadyBeenFound = obj[arr[i]];
if (hasItAlreadyBeenFound!==undefined) {
Logger.log('Index: ' + i + " is a dup!");
indexPositionsToDelete.push(i);
};
obj[arr[i]]=0;
}
//This creates an array of which VALUES are duplicates. NOT there index number.
//For this "use case", this following section of code is not needed, but for the sake of demonstration I've left it in.
for (i in obj) {
out.push(i);
}
//return out;
Logger.log('Output: ' + out);
//Log what indexes to delete
Logger.log('indexPositionsToDelete: ' + indexPositionsToDelete);
//Delete the value in every cell that is a duplicate
for (i=0;i<indexPositionsToDelete.length;i+=1) {
//To Do - get the range - delete the cell value
//Match the index value of the array to the row number, which may be different
};
}