我正在尝试在Google表格中编写一个脚本:
给出以下电子表格:
我有以下功能开始接近,但不是很完整。
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet2');
var myStats = sheet.getRange('b2:k2');
var myVals = myStats.getValues();
var opps = sheet.getRange('b4:k');
var oppsVals = opps.getValues();
for (var i = 0; i <= 11; i++) {
for (var x in oppsVals[i]){
if (oppsVals[i][x] > myVals[0][x]) {
// make opponent cell green
} else if (oppsVals[i][x] == myVals[0][x]) {
// make opponent cell gray
} else {
// make opponent cell red
}
}
// calculate score (i.e. 6-3-1, 7-3-0, etc)
}
};
如果该功能正在通过第4行并比较统计数据,则对手单元格将在B,C,D,E,G,I,J,K列中为绿色(因为它们赢了)。 F列为红色(因为我赢了),H列为灰色(领带)。
L列将以最终得分填充:1-8-1(对手获胜,我的胜利,平局)
正如您可能已经注意到的那样,列J&amp; D中还有另一个皱纹。确定K以较低的值赢得。
感谢任何帮助!
答案 0 :(得分:0)
我能够使用以下功能完成此工作。希望这可能对后来偶然发现这一点的人有用。
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet2');
var myStats = sheet.getRange('b2:k2');
var myVals = myStats.getValues();
var opps = sheet.getRange('b4:k');
var oppsVals = opps.getValues();
for (var i = 0; i <= 10; i++) {
var wins = [];
var losses = [];
var ties = [];
for (var x in oppsVals[i]){
var alphas = ["b","c","d","e","f","g","h","i","j", "k", "l"];
var nums = ['4','5','6','7','8','9','10','11','12','13','14'];
var cell = alphas[x] + nums[i];
if ( alphas[x] == 'j' || alphas[x] == 'k' ) {
if (oppsVals[i][x] < myVals[0][x]) {
sheet.getRange(cell).setFontWeight('normal');
losses.push(cell);
} else if (oppsVals[i][x] == myVals[0][x]) {
sheet.getRange(cell).setFontStyle('italic');
ties.push(cell);
} else {
sheet.getRange(cell).setFontWeight('bold');
wins.push(cell);
}
} else {
if (oppsVals[i][x] < myVals[0][x]) {
sheet.getRange(cell).setFontWeight('bold');
wins.push(cell);
} else if (oppsVals[i][x] == myVals[0][x]) {
sheet.getRange(cell).setFontStyle('italic');
ties.push(cell);
} else {
sheet.getRange(cell).setFontWeight('normal');
losses.push(cell);
}
}
}
var winTotal = wins.length;
var lossesTotal = losses.length;
var tiesTotal = ties.length;
sheet.getRange("l" + nums[i]).setValue(winTotal + "-" + lossesTotal + "-" + tiesTotal);
if ( winTotal > lossesTotal ) {
sheet.getRange("l" + nums[i]).setFontWeight('bold').setBackground('green');
}
if ( winTotal < lossesTotal ) {
sheet.getRange("l" + nums[i]).setFontWeight('bold').setBackground('red');
}
if ( winTotal == lossesTotal ) {
if ( oppsVals[i][0] < myVals[0][0] ) {
sheet.getRange("l" + nums[i]).setFontWeight('bold').setBackground('green');
} else {
sheet.getRange("l" + nums[i]).setFontWeight('bold').setBackground('red');
}
}
}
};