Google apps script 我正试图编写一个程序,它将来自谷歌表单的输入与电子表格中的列表相匹配,并将数量加在一起
我试图匹配两个变量(一个来自表单提交,一个在数据库中),然后将它们的数量相加。有什么建议吗 起初我试着用相反的方法做。这是让列表(超过2000件)从表单提交中搜索数据,然后将其添加到一起。我意识到这样做会太滞后,因此这个解决方案,但我不知道如何开始Google apps script 我正试图编写一个程序,它将来自谷歌表单的输入与电子表格中的列表相匹配,并将数量加在一起,google-apps-script,Google Apps Script,我试图匹配两个变量(一个来自表单提交,一个在数据库中),然后将它们的数量相加。有什么建议吗 起初我试着用相反的方法做。这是让列表(超过2000件)从表单提交中搜索数据,然后将其添加到一起。我意识到这样做会太滞后,因此这个解决方案,但我不知道如何开始 if(input1 == database1){ var qty_database1 = qty_database1 + qty_input1 ; } if(input1 == database2){ var qty_database2 =
if(input1 == database1){
var qty_database1 = qty_database1 + qty_input1 ;
}
if(input1 == database2){
var qty_database2 = qty_database2 + qty_input1 ;
}
通过这种方式,我必须将35个输入(全部来自表单提交,并非所有35个都有值)与2000多个变量(取自数据库)进行检查,这是无效的。我尝试在网上搜索,但似乎找不到我需要的
以下是链接:
“部门总缺陷”是数据库,所有信息都将存储在该数据库中。
其他选项卡都是3个表单提交电子表格。
如果在第1/2/3 AQL中有一个类似的“S号”和“ID号”,它将只接受最后一组值,顺序为第3、第2和最后1。听起来您只是想要表单响应中的值透视表。尝试突出显示您的数据,然后转到数据>透视表,并在那里使用它。我认为您根本不想在这里使用脚本函数。下面的答案可能看起来太长了,但关键的两行是:
var-defectcodes=defectvalues.map(函数(行){返回行[0];})代码>
var c=defectcodes.indexOf(respcode)代码>
- 更改“总缺陷”表的布局。
五列集合布局是一个不必要的复杂问题。布局应简化为一组2100行的单列 - 简化搜索/匹配。
不要匹配两个单独的值(“Department”和“Defect”),而是通过连接这些值来创建一个唯一的代码,并将其用于搜索/匹配 - 使用“map”和“indexof”的Javascript方法简化处理。
功能so5652371304(){
//设置缺陷电子表格
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheetname=“TotalDefects02”;
var defectsheet=ss.getSheetByName(sheetname);
//Logger.log(“调试:缺陷工作表名称”+defectsheet.getName());//调试
var defectlastrow=defectsheet.getLastRow();
//Logger.log(“调试:最后一个缺陷行=“+defectlastrow”);//调试
//总缺陷的布局为:
//行#1=标题
//A列=部门
//B列=缺陷
//列C=连接列A和列B
//D列=数量
var defectrange=defectsheet.getRange(2,3,defectlastrow-1);//第3列(C)-部门+代码
//Logger.log(“调试:缺陷范围是”+defectrange.getA1Notation());//调试
var defectvalues=defectrange.getValues();
//应用“映射”方法将行号分配给每个元素
var-defectcodes=defectvalues.map(函数(行){返回行[0];});
//设置响应表
var respsheetname=“第一AQL”;//第一AQL//第二AQL//第三AQL
var respsheet=ss.getSheetByName(respsheetname);
//Logger.log(“调试:响应表名称”+respsheet.getName());//调试
var resplastrow=respsheet.getLastRow();
var resplastcolumn=respsheet.getLastColumn();
//Logger.log(“调试:响应lastrow=“+resplastrow+”,最后一列“+resplastcolumn”);//调试
//总缺陷的布局为:
//第1行和第2行=标题
//A列至Q列(含)=无关信息(第1列至第17列含)
//列R到W=响应数据集#1
//R列(18)=部门#1
//S(19)列=缺陷#1
//第T(20)列=数量
//列U(21)=不相关
//第V(22)列=不相关
//W(23)列=不相关
//第X(24)至HR(226)列由另外34个可能的缺陷通知组成,每列6个
var resprange=respsheet.getRange(3,18,resplastrow-2,resplastcolumn-18+1);
//Logger.log(“调试:resp范围是”+resprange.getA1Notation());//调试
var respvalues=resprange.getValues();
//设置响应变量
var repliesperrow=35;
var-columnspereply=6;
//在回复中循环查找数据库中的匹配项
对于(var r=0;r-1),则代码位于TotalDefects表中,匹配代码的相关行号=c+2
如果(c>-1){
Logger.log(“调试:r:+r+”,i:+i+”,c=“+c+”,代码:“+respcode+”,在缺陷数据库的第行:”+(c+2));//调试详细信息在第(c+2)行
}否则{
Logger.log(“调试:r:+r+”,i:+i+,c=“+c+”,代码:“+respcode+”未在缺陷数据库中找到”);//调试
}
}
否则{
持续
}
}
}
}
布局总缺陷表
function so5652371304() {
// setup Defects spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetname = "TotalDefects02";
var defectsheet = ss.getSheetByName(sheetname);
//Logger.log("DEBUG: Defect sheet name "+defectsheet.getName());//DEBUG
var defectlastrow = defectsheet.getLastRow();
//Logger.log("DEBUG: the last defect row = "+defectlastrow);//DEBUG
// Layout on Total Defects is:
// Row#1 = Header
// Column A = Department
// Column B = Defect
// Column C = Concatenate Column A and Column B
// Column D = Qty
var defectrange = defectsheet.getRange(2,3,defectlastrow-1);// column 3 (C)- dept+code
//Logger.log("DEBUG: the defect range is "+defectrange.getA1Notation());//DEBUG
var defectvalues = defectrange.getValues();
// apply the "map" method to assign the row number to every element
var defectcodes = defectvalues.map(function (row) { return row[0]; });
// set up the response sheets
var respsheetname = "1st AQL";// 1st AQL// 2nd AQL // 3rd AQL
var respsheet = ss.getSheetByName(respsheetname);
//Logger.log("DEBUG: Response sheet name "+respsheet.getName());//DEBUG
var resplastrow = respsheet.getLastRow();
var resplastcolumn = respsheet.getLastColumn();
//Logger.log("DEBUG: Response lastrow = "+resplastrow+", last column"+resplastcolumn); //DEBUG
// Layout on Total Defects is:
// Row#1 & 2 = Header
// Column A to Q (Inclusive) = Irrelevant information (Column 1 to 17 inclusive)
// Column R to W = Response dataset#1
// Column R (18) = Department#1
// Column S (19) = Defect#1
// Column T (20) = Quantity
// Column U (21) = Irrelevant
// Column V (22) = Irrelevant
// Column W (23) = Irreelvant
// Column X (24) to HR (226) consists of a further 34 possible defect notices, each of 6 columns
var resprange = respsheet.getRange(3,18,resplastrow-2,resplastcolumn-18+1);
//Logger.log("DEBUG: the resp range is "+resprange.getA1Notation());//DEBUG
var respvalues = resprange.getValues();
// set Response variables
var repliesperrow=35;
var columnsperreply = 6;
//Loop through replies to find matches in the database
for (var r = 0;r<resplastrow-2;r++){//loop rows
for (var i=0; i<repliesperrow;i++){//loop per reply
if (respvalues[r][(i*columnsperreply)].length>0){
//Logger.log("DEBUG: dept = "+respvalues[r][(i*columnsperreply)]+", defect = "+respvalues[r][((i*columnsperreply)+1)]+", qty = "+respvalues[r][(i*columnsperreply)+2]); //DEBUG
var respcode = respvalues[r][(i*columnsperreply)].concat('',respvalues[r][((i*columnsperreply)+1)]);
var c = defectcodes.indexOf(respcode);
// if c=-1 then the code is not in TotalDefects Table
// if c>-1 then the code IS in the TotalDefects Table, AND the relevant row number of the matching code = c+2
if (c > -1) {
Logger.log("DEBUG: r:"+r+", i:"+i+", c="+c+", Code:"+respcode+" found in the defects database on row:"+(c+2)); // DEBUG the details are on (c+2) rows
} else {
Logger.log("DEBUG: r:"+r+", i:"+i+", c="+c+", Code:"+respcode+" NOT found in the defects database"); //DEBUG
}
}
else{
continue;
}
}
}
}
function so5652371306() {
// setup Defects spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetname = "TotalDefects06";
var defectsheet = ss.getSheetByName(sheetname);
//Logger.log("DEBUG: Defect sheet name "+defectsheet.getName());//DEBUG
var defectlastrow = defectsheet.getLastRow();
//Logger.log("DEBUG: the last defect row = "+defectlastrow);//DEBUG
// Layout on Total Defects is:
// Row#1 = Header
// Column A = Department
// Column B = Defect
// Column C = Concatenate Column A and Column B
// Column D = Qty
// get 2 x ranges and data
// defectrange = enables the code to access the existing value of the qty
var defectrange = defectsheet.getRange(2,3,defectlastrow-1,2);// column C - dept+code // column D = qty
//Logger.log("DEBUG: the defect range is "+defectrange.getA1Notation());//DEBUG
var defectvalues = defectrange.getValues();
// defectqtyrange = enables the code to increment the qty andf then, as the last command, paste the adjusted values back onto the spreadsheet
// this avoids setValue within the Response loops.
var defectqtyrange = defectsheet.getRange(2,4,defectlastrow-1,1);// column D - qty
var defectqtyvalues = defectqtyrange.getValues();
// apply the "map" method to assign the row number to every element
var defectcodes = defectvalues.map(function (row) { return row[0]; });
// set up an array of response sheets
var respsheetname = [
'1st AQL',
'2nd AQL',
'3rd AQL'
];
// Layout of Response Sheets is:
// Row#1 & 2 = Header
// Column A to Q (Inclusive) = Irrelevant information (Column 1 to 17 inclusive)
// Column R to W = Response dataset#1
// Column R (18) = Department#1
// Column S (19) = Defect#1
// Column T (20) = Quantity
// Column U (21) = Irrelevant
// Column V (22) = Irrelevant
// Column W (23) = Irreelvant
// Column X (24) to HR (226) consists of a further 34 possible defect notices, each of 6 columns
// set Response variables
var repliesperrow = 35;
var columnsperreply = 6;
// loop through the response sheets
for (var t = 0; t < respsheetname.length; t++) {
var respsheet=ss.getSheetByName(respsheetname[t]);
var thissheet = respsheet.getName();
// Logger.log("DEBUG: Response sheet name "+thissheet);//DEBUG
var resplastrow = respsheet.getLastRow();
var resplastcolumn = respsheet.getLastColumn();
//Logger.log("DEBUG: Response lastrow = "+resplastrow+", last column"+resplastcolumn); //DEBUG
// define the range
var resprange = respsheet.getRange(3,18,resplastrow-2,resplastcolumn-18+1);
//Logger.log("DEBUG: the resp range is "+resprange.getA1Notation());//DEBUG
// get the response data
var respvalues = resprange.getValues();
//Loop through replies to find matches in the database
for (var r = 0;r<resplastrow-2;r++){//loop rows
for (var i=0; i<repliesperrow;i++){//loop per reply
if (respvalues[r][(i*columnsperreply)].length>0){
var respcode = respvalues[r][(i*columnsperreply)].concat('',respvalues[r][((i*columnsperreply)+1)]);
var c = defectcodes.indexOf(respcode);
// if c=-1 then the code is not in TotalDefects Table
// if c>-1 then the code IS in the TotalDefects Table, AND the relevant row number of the matching code = c+2
if (c > -1) {
// display this for the successful matches
//Logger.log("DEBUG: RESPONSE FOUND IN THE DATABASE. \nSheet:"+thissheet+", response row:"+(+r+1)+", response#:"+(+i+1)+", indexOf:"+c+", Code:"+respcode+", Qty:"+respvalues[r][(i*columnsperreply)+2]); // DEBUG the details are on (c+2) rows
// display this for matching row in the Total Defects
//Logger.log("DEBUG: Corresponding Defect. Code:"+defectvalues[c][0]+", Qty:"+defectvalues[c][1]+", SpreadsheetRow:"+(+c+2));
// increment the adjusted Total Defect quantity
defectqtyvalues[c][0] = Number(defectqtyvalues[c][0])+Number(respvalues[r][(i*columnsperreply)+2]);
} else {
// display this for failed matches
// Logger.log("DEBUG: Response: Code not found in Total Defects. \nSheet:"+thissheet+", response row:"+(+r+1)+", response#:"+(+i+1)+", Code:"+respcode); //DEBUG
}
}
else{
continue;
}
}
}
// update the defect range with the adjusted qty
defectqtyrange.setValues(defectqtyvalues);
}
}