感谢从这里收集的信息,以及其他帖子的直接帮助。像我这样的白痴已经设法形成类似脚本的东西:)
它完全符合正确的结果。快乐的时光。 但不幸的是,它可能需要一段时间才能执行(部分原因是它的速度很慢,是google工作表中的缓慢公式,希望用像这样的按需脚本替换公式并且放弃最庞大的公式)
直到我进入那个阶段,我希望有人会看一眼,看看是否有任何开销/膨胀/处理可以保存。
我目前的想法是,我不止一次检查数据,当我可以尝试同时检查所有数据时。
我也写了几次,也许最好把返回的值放在一个数组中并写一次。
所以我试着调查一下,但我仍然很新。
代码只根据值和日期计算表单响应,然后将结果输出到报告中。
function snapshot() {
var dateColumn = 2;
var findColumn = 4;
var toFind = "Yes - Complete (going ahead)";
var daysWorth = 31;
var target = "Snapshot";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var targetSheet = ss.getSheetByName(target);
var sheet = ss.getSheetByName('Form Responses');
var values = sheet.getDataRange().getValues();
var today = new Date();
var thisMorning = today;
thisMorning.setHours(0);
thisMorning.setMinutes(0);
var completed = 0;
var attempted = 0;
var totAtt = 0;
var totCom = 0;
for (var i = 0; i < daysWorth; i++){
var startGap = new Date().setTime(thisMorning.getTime() - i*24*60*60*1000);
var endGap = new Date().setTime(thisMorning.getTime() - (i-1)*24*60*60*1000);
for(var counter in values){
var testDate = new Date(values[counter][dateColumn -1]);
if (testDate > startGap && testDate < endGap && values[counter][findColumn -1] == toFind)
{completed++;
totCom++;}
if (testDate > startGap && testDate < endGap)
{attempted++;
totAtt++}
}
var output = new Date(startGap);
//Logger.log("since "+output+" we had: "+completed+" completed and: "+attempted+" attempted");
targetSheet.getRange(i+2,1).setValue(output);
targetSheet.getRange(i+2,2).setValue(attempted);
targetSheet.getRange(i+2,3).setValue(completed);
targetSheet.getRange(i+2,4).setValue(completed/attempted*100);
var completed = 0;
var attempted = 0;
}
targetSheet.getRange(1,1).setValue("Date");
targetSheet.getRange(1,2).setValue("Attempted");
targetSheet.getRange(1,3).setValue("Completed");
targetSheet.getRange(1,4).setValue("Success Rate");
targetSheet.getRange(33,1).setValue("Totals");
targetSheet.getRange(33,2).setValue(totAtt);
targetSheet.getRange(33,3).setValue(totCom);
targetSheet.getRange(33,4).setValue(totCom/totAtt*100);
}
感谢您阅读:)
修改
所以这里是建议的编辑和工作。
function snapshot() {
var dateColumn = 2;
var findColumn = 4;
var toFind = "Yes - Complete (going ahead)";
var daysWorth = 31;
var target = "Snapshot";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var targetSheet = ss.getSheetByName(target);
var sheet = ss.getSheetByName('Form Responses');
var values = sheet.getDataRange().getValues();
var today = new Date();
var thisMorning = today;
thisMorning.setHours(0);
thisMorning.setMinutes(0);
var archive = [];
var completed = 0;
var attempted = 0;
var totAtt = 0;
var totCom = 0;
for (var i = 0; i < daysWorth; i++){
var startGap = new Date().setTime(thisMorning.getTime() - i*24*60*60*1000);
var endGap = new Date().setTime(thisMorning.getTime() - (i-1)*24*60*60*1000);
for(var counter in values){
var testDate = new Date(values[counter][dateColumn -1]);
if (testDate > startGap && testDate < endGap && values[counter][findColumn -1] == toFind)
{completed++;
totCom++;}
if (testDate > startGap && testDate < endGap)
{attempted++;
totAtt++}
}
var output = new Date(startGap);
if(!completed/attempted)
{var success = 0;}
else
{var success = completed/attempted*100;}
archive.push( [output, attempted, completed,success] );
var completed = 0;
var attempted = 0;
}
var headers =[["Date","Attempted","Completed","Success Rate"]];
var footers =[["Totals",totAtt,totCom,totCom/totAtt*100]];
targetSheet.getRange(1,1,1,4).setValues(headers);
targetSheet.getRange(2,1).offset(0, 0, archive.length, archive[0].length).setValues(archive);
targetSheet.getRange(33,1,1,4).setValues(footers);
}
感谢您的反馈意见,如果有人的话,我会全力以赴。
答案 0 :(得分:1)
“将返回的值放入数组并写入一次可能会更好。” - 是的,必须弄清楚如何.setValues()一次。
targetSheet.getRange第(i + 2,1).setValue(输出);
targetSheet.getRange第(i + 2,2).setValue(尝试);
targetSheet.getRange第(i + 2,3).setValue(完成);
targetSheet.getRange第(i + 2,4).setValue(完成/试图* 100);
应该有点像......
myArray.push([输出,尝试,完成,完成/尝试* 100]);
然后在循环之外...
targetSheet.getRange('A1')。offset(0,0,myArray.length,myArray [0] .length)。setValues(myArray);