我有以下电子表格。
我需要一个脚本来计算每个代理电子邮件地址(整体并基于客户端)。我正在努力使用数组。我是脚本新手,所以还在学习。因此,在“报告”选项卡上,我需要知道代理进行了多少次呼叫,然后知道他为客户端1,客户端2等进行了多少次呼叫,进行了多少次预约 - 总体和每个客户。我不想使用=COUNTIFS()
,因为这将是动态的,并且会有新代理。
我将创建一个脚本来获取唯一的电子邮件并将其粘贴到表格中,然后我需要这个脚本来计算。
在共享文件中我有我的脚本,但我卡在那里。
谢谢!
答案 0 :(得分:1)
我在报告表中添加了一些范围,以便显示所有客户数据。客户端名称和代理电子邮件必须存在,因为它们在我的数据数组和对象中用作输入。
以下是代码:
function reportSummary()//This is final output
{
var br='<br />';
var agentA=uniqueItemArray(4);
var clientA=uniqueItemArray(5);
var clientAgents=emailAddressAssociatedWithEachClient();
var agentClients=clientsAssociatedWithEachEmail();
var callsByAgent=callsMadeByAgentTotal();
var callsByAgentToClient=callsMadeByAgentToClient();
var apptsByAgent=apptsBookedByAgentTotal();
var apptsByAgentForClient=apptsBookedByAgentForClient();
var ss=SpreadsheetApp.getActive();
var rptSh=ss.getSheetByName('Reports');
var rgA=["A2:C6","A8:C12","A14:C18"];
for(var i=0;i<rgA.length;i++)
{
var rg1=rptSh.getRange(rgA[i]);
var vA1=rg1.getValues();
vA1[2][1]=callsByAgentToClient[vA1[2][0]][vA1[0][0]];
vA1[3][1]=callsByAgentToClient[vA1[3][0]][vA1[0][0]];
vA1[4][1]=callsByAgentToClient[vA1[4][0]][vA1[0][0]];
vA1[2][2]=apptsByAgentForClient[vA1[2][0]][vA1[0][0]];
vA1[3][2]=apptsByAgentForClient[vA1[3][0]][vA1[0][0]];
vA1[4][2]=apptsByAgentForClient[vA1[4][0]][vA1[0][0]];
rg1.setValues(vA1);
}
var rg2=rptSh.getRange('E2:H6');
var vA2=rg2.getValues();
vA2[2][1]=callsByAgent[vA2[2][0]];
vA2[3][1]=callsByAgent[vA2[3][0]];
vA2[4][1]=callsByAgent[vA2[4][0]];
vA2[2][2]=apptsByAgent[vA2[2][0]];
vA2[3][2]=apptsByAgent[vA2[3][0]];
vA2[4][2]=apptsByAgent[vA2[4][0]];
rg2.setValues(vA2);
/*
var s='<strong>Clients Contacted by Agent:</strong>';
for(var i=0;i<agentA.length;i++)
{
s+=br + '<span style="color:#ff0000">' + agentA[i] + '</span>';
for(var j=0;j<agentClients[agentA[i]].length;j++)
{
s+=br + agentClients[agentA[i]][j];
}
}
s+=br + br + '<strong>Agents who Contacted Client:</strong>';
for(var i=0;i<clientA.length;i++)
{
s+=br + '<span style="color:#ff0000">' + clientA[i] + '</span>';
for(var j=0;j<clientAgents[clientA[i]].length;j++)
{
s+=br + clientAgents[clientA[i]][j];
}
}
s+=br + br + '<strong>Calls made by Agents:</strong>';
for(var i=0;i<agentA.length;i++)
{
s+=br + Utilities.formatString('<br />%s = %s',agentA[i],callsByAgent[agentA[i]]);
}
s+=br + br + '<strong>Calls made by Agents to Client:</strong>';
for(var i=0;i<agentA.length;i++)
{
for(var j=0;j<clientA.length;j++)
{
s+=br + Utilities.formatString('<br />%s<br />%s<br />%s',agentA[i],clientA[j],callsByAgentToClient[agentA[i]][clientA[j]])
}
}
var userInterface=HtmlService.createHtmlOutput(s);
SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Report Summary');
*/
}
function uniqueItemArray(column)
{
var ss=SpreadsheetApp.getActive();
var cntSh=ss.getSheetByName('Count');
var cntRg=cntSh.getDataRange();
var vA=cntRg.getValues();
var uiA=[];
for(var i=2;i<vA.length;i++)
{
if(uiA.indexOf(String(vA[i][column-1]).trim())==-1)
{
uiA.push(vA[i][column-1]);
}
}
return uiA;
}
function emailAddressAssociatedWithEachClient()
{
var agentEmailsA=uniqueItemArray(4);
var clientA=uniqueItemArray(5);
var ss=SpreadsheetApp.getActive();
var cntSh=ss.getSheetByName('Count');
var cntRg=cntSh.getDataRange();
var vA=cntRg.getValues();
var cEO=[];
for(var i=0;i<clientA.length;i++)
{
var ceA=[];
for(var j=2;j<vA.length;j++)
{
if(ceA.indexOf(String(vA[j][3]).trim())==-1 && String(vA[j][4]).trim()==clientA[i])
{
ceA.push(vA[j][3]);
}
}
cEO[clientA[i]]=ceA;
}
/*
for(var n=0;n<clientA.length;n++)
{
Logger.log(clientA[n] + '\n' + cEO[clientA[n]]);
}
*/
return cEO;
}
function clientsAssociatedWithEachEmail()
{
var agentEmailsA=uniqueItemArray(4);
var clientA=uniqueItemArray(5);
var ss=SpreadsheetApp.getActive();
var cntSh=ss.getSheetByName('Count');
var cntRg=cntSh.getDataRange();
var vA=cntRg.getValues();
var cEO=[];
for(var i=0;i<agentEmailsA.length;i++)
{
var ceA=[];
for(var j=2;j<vA.length;j++)
{
if(ceA.indexOf(String(vA[j][4]).trim())==-1 && String(vA[j][3]).trim()==agentEmailsA[i])
{
ceA.push(vA[j][4]);
}
}
cEO[agentEmailsA[i]]=ceA;
}
/*
for(var n=0;n<agentEmailsA.length;n++)
{
Logger.log(agentEmailsA[n] + '\n' + cEO[agentEmailsA[n]]);
}
*/
return cEO;
}
function callsMadeByAgentTotal()
{
var agentA=uniqueItemArray(4);
var clientA=uniqueItemArray(5);
var ss=SpreadsheetApp.getActive();
var cntSh=ss.getSheetByName('Count');
var cntRg=cntSh.getDataRange();
var vA=cntRg.getValues();
var callsByAgent=[];
for(var i=0;i<agentA.length;i++)
{
var cnt=0;
for(var j=2;j<vA.length;j++)
{
if(vA[j][3]==agentA[i])
{
cnt++;
}
}
callsByAgent[agentA[i]]=cnt;
}
/*
for(var i=0;i<agentA.length;i++)
{
Logger.log('\n%s\n%s',agentA[i],callsByAgent[agentA[i]]);
}
var s=Logger.getLog().replace(/^.+:/gm,'').replace(/\n/g,'<br />');
var userInterface=HtmlService.createHtmlOutput(s);
SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Calls By Agent')
*/
return callsByAgent;
}
function callsMadeByAgentToClient()
{
var agentA=uniqueItemArray(4);
var clientA=uniqueItemArray(5);
var ss=SpreadsheetApp.getActive();
var cntSh=ss.getSheetByName('Count');
var cntRg=cntSh.getDataRange();
var vA=cntRg.getValues();
var callsByAgentToClient=[];
for(var i=0;i<agentA.length;i++)
{
callsByAgentToClient[agentA[i]]=[];
for(var k=0;k<clientA.length;k++)
{
var cnt=0;
for(var j=2;j<vA.length;j++)
{
if(vA[j][3]==agentA[i] && vA[j][4]==clientA[k])
{
cnt++;
}
}
callsByAgentToClient[agentA[i]][clientA[k]]=cnt;
}
}
/*
for(var i=0;i<agentA.length;i++)
{
for(var j=0;j<clientA.length;j++)
{
Logger.log('\n%s\n%s\n%s',agentA[i],clientA[j],callsByAgentToClient[agentA[i]][clientA[j]]);
}
}
var s=Logger.getLog().replace(/^.+:/gm,'').replace(/\n/g,'<br />');
var userInterface=HtmlService.createHtmlOutput(s);
SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Calls By Agent To Client')
*/
return callsByAgentToClient;
}
function apptsBookedByAgentTotal()
{
var agentA=uniqueItemArray(4);
var clientA=uniqueItemArray(5);
var ss=SpreadsheetApp.getActive();
var cntSh=ss.getSheetByName('Count');
var cntRg=cntSh.getDataRange();
var vA=cntRg.getValues();
var apptsByAgent=[];
for(var i=0;i<agentA.length;i++)
{
var cnt=0;
for(var j=2;j<vA.length;j++)
{
if(vA[j][3]==agentA[i] && vA[j][5])
{
cnt++;
}
}
apptsByAgent[agentA[i]]=cnt;
}
/*
for(var i=0;i<agentA.length;i++)
{
Logger.log('\n%s\n%s',agentA[i],apptsByAgent[agentA[i]]);
}
var s=Logger.getLog().replace(/^.+:/gm,'').replace(/\n/g,'<br />');
var userInterface=HtmlService.createHtmlOutput(s);
SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Calls By Agent')
*/
return apptsByAgent;
}
function apptsBookedByAgentForClient()
{
var agentA=uniqueItemArray(4);
var clientA=uniqueItemArray(5);
var ss=SpreadsheetApp.getActive();
var cntSh=ss.getSheetByName('Count');
var cntRg=cntSh.getDataRange();
var vA=cntRg.getValues();
var apptsByAgentForClient=[];
for(var i=0;i<agentA.length;i++)
{
apptsByAgentForClient[agentA[i]]=[];
for(var k=0;k<clientA.length;k++)
{
var cnt=0;
for(var j=2;j<vA.length;j++)
{
if(vA[j][3]==agentA[i] && vA[j][4]==clientA[k] && vA[j][5])
{
cnt++;
}
}
apptsByAgentForClient[agentA[i]][clientA[k]]=cnt;
}
}
/*
for(var i=0;i<agentA.length;i++)
{
for(var j=0;j<clientA.length;j++)
{
Logger.log('\n%s\n%s\n%s',agentA[i],clientA[j],apptsByAgentForClient[agentA[i]][clientA[j]]);
}
}
var s=Logger.getLog().replace(/^.+:/gm,'').replace(/\n/g,'<br />');
var userInterface=HtmlService.createHtmlOutput(s);
SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Calls By Agent To Client')
*/
return apptsByAgentForClient;
}
这是我的报告表的图像:
签出reportSummaryNew()它创建一个新工作表生成报告正文并填写所有数据。一个名为buildReport()的独立函数构建报告并返回reportSummaryNew()的范围数组。
function buildReport()
{
var br='<br />';
var agentA=uniqueItemArray(4);
var clientA=uniqueItemArray(5);
var ss=SpreadsheetApp.getActive();
var sh=ss.insertSheet();
var shName=sh.getName();
sh.activate();
var rngA=[];
for(var i=0;i<=clientA.length;i++)
{
rngA[i]=sh.getRange(i*(agentA.length + 2) + 1,1,agentA.length + 2,3);
var vA=rngA[i].getValues();
if(i!=clientA.length)
{
vA[0][0]=clientA[i];
}
else
{
vA[0][0]='All';
}
vA[1][1]='Calls Made';
vA[1][2]='Appointments Booked';
for(var j=0;j<agentA.length;j++)
{
vA[j+2][0]=agentA[j];
}
rngA[i].setValues(vA);
}
return rngA;
}
function reportSummaryNew()
{
var br='<br />';
var agentA=uniqueItemArray(4);
var clientA=uniqueItemArray(5);
var clientAgents=emailAddressAssociatedWithEachClient();
var agentClients=clientsAssociatedWithEachEmail();
var callsByAgent=callsMadeByAgentTotal();
var callsByAgentToClient=callsMadeByAgentToClient();
var apptsByAgent=apptsBookedByAgentTotal();
var apptsByAgentForClient=apptsBookedByAgentForClient();
var rngA=buildReport();
var ss=SpreadsheetApp.getActive();
var rptSh=ss.getActiveSheet();
for(var i=0;i<rngA.length-1;i++)
{
var rg1=rngA[i];
var vA1=rg1.getValues();
vA1[2][1]=callsByAgentToClient[vA1[2][0]][vA1[0][0]];
vA1[3][1]=callsByAgentToClient[vA1[3][0]][vA1[0][0]];
vA1[4][1]=callsByAgentToClient[vA1[4][0]][vA1[0][0]];
vA1[2][2]=apptsByAgentForClient[vA1[2][0]][vA1[0][0]];
vA1[3][2]=apptsByAgentForClient[vA1[3][0]][vA1[0][0]];
vA1[4][2]=apptsByAgentForClient[vA1[4][0]][vA1[0][0]];
rg1.setValues(vA1);
}
var rg2=rngA[rngA.length-1];
var vA2=rg2.getValues();
vA2[2][1]=callsByAgent[vA2[2][0]];
vA2[3][1]=callsByAgent[vA2[3][0]];
vA2[4][1]=callsByAgent[vA2[4][0]];
vA2[2][2]=apptsByAgent[vA2[2][0]];
vA2[3][2]=apptsByAgent[vA2[3][0]];
vA2[4][2]=apptsByAgent[vA2[4][0]];
rg2.setValues(vA2);
}
以下是新生成的报告的外观:
如果您希望能够格式化报告,可以运行buildReport并将其中一个部分移动到左上角A1:C5并按照您喜欢的方式对其进行格式化,然后替换此行{{1}在reportSummaryNew()中使用此var rngA=buildReport()
是只需在't'和'('之间添加1。buildReport1()函数将从Format!A1:A5复制格式,以便您可以轻松更改格式你想要的时间只需格式化名为'格式'的工作表的内容;
这是buildReport1()
var rngA=buildReport1()
这是我如何格式化它的图像。