在google电子表格中,我使用javascript创建了一个嵌套的2D数组。
工作表'小时'是员工为不同类别添加不同时间的地方。 这是一个带有submit()函数触发器的图像。 “批准”表单从“小时”表单中接收一些数据,经理可以批准这些数据。
这是脚本:
function cleanArray(actual){
var newArray = new Array();
for(var i = 0; i<actual.length; i++){
if(actual[i] != "") {
newArray.push(actual[i]);
};
};
return newArray;
}
function addArray(array){
var sum = 0;
for(var i=0; i<array.length; i++) {
sum += Number(array[i]);
};
return sum;
}
function createArray(cats, hours) {
var newArray = new Array();
for (i in hours) {
if(hours[i] != "" ) {
if(cats[i] == '') { cats[i] = "Unclaimed" };
newArray[i] = new Array(cats[i], hours[i]);
};
};
return newArray;
}
function submit() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Hours');
var employee = sheet.getRange('B1').getValue();
var startDate = sheet.getRange('C2').getValue();
var mondayHours = sheet.getRange('B4:B9').getValues();
var mondayCats = sheet.getRange('D4:D9').getValues();
var mondayArray = createArray(mondayCats, mondayHours);
var mondayTtl = addArray(mondayHours);
var tuesdayHours = sheet.getRange('B11:B16').getValues();
var tuesdayCats = sheet.getRange('D11:D16').getValues();
var tuesdayArray = createArray(tuesdayCats, tuesdayHours);
var tuesdayTtl = addArray(tuesdayHours);
var wednesdayHours = sheet.getRange('B18:B23').getValues();
var wednesdayCats = sheet.getRange('D18:D23').getValues();
var wednesdayArray = createArray(wednesdayCats, wednesdayHours);
var wednesdayTtl = addArray(wednesdayHours);
var thursdayHours = sheet.getRange('B25:B30').getValues();
var thursdayCats = sheet.getRange('D25:D30').getValues();
var thursdayArray = createArray(thursdayCats, thursdayHours);
var thursdayTtl = addArray(thursdayHours);
var fridayHours = sheet.getRange('B32:B37').getValues();
var fridayCats = sheet.getRange('D32:D37').getValues();
var fridayArray = createArray(fridayCats, fridayHours);
var fridayTtl = addArray(fridayHours);
var saturdayHours = sheet.getRange('B39:B44').getValues();
var saturdayCats = sheet.getRange('D39:D44').getValues();
var saturdayArray = createArray(saturdayCats, saturdayHours);
var saturdayTtl = addArray(saturdayHours);
var sundayHours = sheet.getRange('B46:B51').getValues();
var sundayCats = sheet.getRange('D46:D61').getValues();
var sundayArray = createArray(sundayCats, sundayHours);
var sundayTtl = addArray(sundayHours);
var weekArray = new Array();
for (i in mondayArray){
weekArray.push(mondayArray[i]);
};
for (i in tuesdayArray){
weekArray.push(tuesdayArray[i]);
};
for (i in wednesdayArray){
weekArray.push(wednesdayArray[i]);
};
for (i in thursdayArray){
weekArray.push(thursdayArray[i]);
};
for (i in fridayArray){
weekArray.push(fridayArray[i]);
};
for (i in saturdayArray){
weekArray.push(saturdayArray[i]);
};
for (i in sundayArray){
weekArray.push(sundayArray[i]);
};
var weekHours = new Array(mondayTtl, tuesdayTtl, wednesdayTtl, thursdayTtl, fridayTtl, saturdayTtl, sundayTtl);
var weekHoursTtl = addArray(weekHours); //CAPTURE
var sortWeekArray = ArrayLib.sort(weekArray, 0, true)
var approve = SpreadsheetApp.getActive().getSheetByName('Approve');
approve.getRange('D1').setValue(sortWeekArray);
}
现在它只是将2D数组转储到'Approve'表的D1中。
示例输出:
ESL | 2
-------------------------
Parent Engagement | 8
-------------------------
School Liaison | 2.75
-------------------------
School Liaison | 6.25
-------------------------
School Liaison | 7.25
-------------------------
Tutoring | 3.25
-------------------------
Tutoring | 4
-------------------------
Tutoring | 4.5
-------------------------
Unclaimed | 2.5
-------------------------
Volunteer Svcs | 2
我想结合col1中的相似项目并将col2中的相似值相加,我想在输出结果之前在javascript中执行此操作。
因此,样本输出将变为:
ESL | 2
-------------------------
Parent Engagement | 8
-------------------------
School Liaison | 16.25
-------------------------
Tutoring | 11.75
-------------------------
Unclaimed | 2.5
-------------------------
Volunteer Svcs | 2
有什么想法吗?我已经在使用2D Arrays Library,但这些功能似乎都不符合要求。
weekArray看起来像是:{ "category", val ; "category", val ; ... }
此外,欢迎任何有关DRYing代码的想法。
答案 0 :(得分:0)
所以,从来没有找到一个JS解决方案,我似乎已经难倒了SO社区中的至少21个成员,但我最终做了以下事情:
var weekArray = new Array();
及后续循环中显示的小时和类别数组(生成上面的第一个表格)lkup.getRange('D2').setValue(weekArray);
=SUMIF($D$2:$D$43,"Volunteer Svcs",$E$2:$E$43)
var categoriesArray = lkup.getRange('F2:K3').getValues();
从那里开始。
我仍然相信有一个更好的方法(和一些DRYer JS这样做),但我没有找到它,你没有发布它。您可以随时发表评论。
答案 1 :(得分:0)
我确信有更好的方法,但这有效。我用你的示例数据测试了它。
function getData(){
var sheet = SpreadsheetApp.getActiveSheet();
var lr=sheet.getLastRow();
var data = sheet.getRange(2,1,lr-1,2).getValues();
var data=data.sort();
data.push(["",""]);
sumValues(data,lr)
}
function sumValues(data,lr){
var newData = new Array();
for(var i=1;i<lr;i++){
if(data[i-1][0] == data[i][0]){
data[i][1]=parseFloat(data[i][1])+parseFloat(data[i-1][1]);
newData.push([data[i][0],data[i][1]]);
i=i+1.
}
else if(data[i-1][0] !== data[i][0]){
newData.push([data[i-1][0],data[i-1][1]])};
}
newData.push(["",""]);
dupcheck(newData);
}
function dupcheck(data){
var dup=0;
var lr=data.length;
var l=data.length-1;
for(var i=0;i<l;i++){
if(data[i+1][0] == data[i][0]){
var dup =dup+ 1};
}
if(dup>0){
sumValues(data,lr)
}
else{
var sheet = SpreadsheetApp.getActiveSheet();
var lr = sheet.getLastRow();
sheet.getRange(2, 1, lr,data[0].length).clear({contentsOnly:true});
sheet.getRange(2, 1, data.length,data[0].length).setValues(data);
}}