我有一個谷歌表,其中包含以下資料 谷歌表“formatfruit”
每個用戶都有一個水果和蔬菜相關聯,我想知道谷歌表“formatfruit”中每個用戶之間的相似度百分比今天我可以將第一個用戶 kevin 與所有其他用戶進行比較,并在另一個谷歌表中回傳他的相似度百分比稱為“火柴果”。
當用戶與 kevin 有共同的水果或蔬菜時,我將值“1”關聯起來,如果用戶沒有共同的水果或蔬菜,我將值“0”關聯起來。出現在 google sheet matchofruit 中的結果在這里 google sheet matchofruit
我使用的代碼如下
function myFunction() {
var formafruit = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("fruit");
var matchofruit = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("matchofruit");
var n = formafruit.getLastRow();
var user1 = formafruit.getRange(2,1).getValues();// name user 1 : kévin
var user2 = formafruit.getRange(3,1).getValues();// name user 2 : mikael
for (var i = 2;i<4;i ) { // i<4 because we have 3 column in formafruit
for (var z = 2;z<n 1;z ) {
matchofruit.getRange(z,1).setValue(user1); // Return the name of the users in the first column
if(formafruit.getRange(2,i).getValue() === formafruit.getRange(z,i).getValue()){ // Compare the fruits and vegetables associated to kévin with the fruits and vegetables associated to each user
matchofruit.getRange(z,i).setValue(1); // Returns 1 if kevin shares at least one fruit or vegetable in common with a user
}
else {
matchofruit.getRange(z,i).setValue(0);
}
}
}
// Calculate the % of common values
for (var p = 0;p<n-1;p ){}
for (var s = 0;s<n-1;s ) {
var scoreforall = matchofruit.getRange(2,2,p,11).getValues()[s]// get the array of all the matches
let sum = 0;
for (let e = 0; e < scoreforall.length; e ) {
sum = scoreforall[e]; // add each array together
}
var sumTotal= Math.round(sum*(100/2)); // convert in percentage each sum
matchofruit.getRange(s 2,4).setValue(sumTotal); // send match values in column 4
}
// Return the result in a sentence
for (var a = 2;a<n 1;a ) {
var usern = formafruit.getRange(a,1).getValues(); //get all the users' emails in the formafruit
var valeurmatch = matchofruit.getRange (a,4).getValues(); // get value % of matches
matchofruit.getRange(a,5).setValue(user1 " " "have" " " valeurmatch "%" " " "of values in common with" " " usern);//Return the % of common value between Kevin and the other users
}
}
I would like to be able to do the same for mikael, gauthier, vanessa and mireille knowing that I only put 5 users to simplify the problem but that in truth there can be more than 100 users and that each user has more than 11 associated values(here we have only 2 different type of values, fruits and vegetables). It's been several weeks that I'm looking for a solution to my problem and I haven't found anything to solve it. Do you have an idea?
Thanks!
uj5u.com熱心網友回復:
我相信你的目標如下。
您要實作以下情況。(以下圖片來自OP的問題。)
從

到

在您的情況下,例如,當使用 5 個用戶時,您希望創建 25 行。
當我看到你的腳本時,回圈中使用了setValuesand的方法。getValues我認為這變成了高昂的工藝成本。參考所以,我想提出以下修改。
修改后的腳本:
function myFunction2() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const [src, dst] = ["fruit", "matchofruit"].map(s => ss.getSheetByName(s));
const [, ...values] = src.getDataRange().getValues();
const res = values.flatMap(([a1, ...v1]) =>
values.map(([a2, ...v2]) => {
const temp = v1.map((e, i) => e == v2[i] ? 1 : 0);
const sum = (temp.filter(f => f == 1).length / temp.length) * 100;
const matchResult = `${a1} have ${sum}% of values in common with ${a2}`;
return [a1, ...temp, sum, matchResult];
})
);
dst.getRange(2, 1, res.length, res[0].length).setValues(res);
}
- 在此修改中,值是從“fruit”表中檢索的。并且,創建了一個用于放入“matchofruit”表的陣列。然后,將創建的陣列放入“matchofruit”表中。
筆記:
- 在這個示例腳本中,“matchofruit”的標題行已經被放置。如果您想將標題行放在“matchofruit”表中,請將其添加到我建議的腳本中。
參考:
- 地圖()
- filter()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/444743.html
