我有一张工作表,其中第一行是天数。第二排和第三排是不同天数内不同公司的日产量。它是这样的。
我的要求是获取2D数组中的天数行和特定公司的生产明细行。
这是我的代码。
function GetData()
{
myFunction("Konabay");
}
function myFunction(Company)
{
var dSheet = SpreadsheetApp.getActiveSpreadsheet();
var aSheet= dSheet.getSheetByName("Data");
var range = aSheet.getDataRange();
var values = range.getValues();
var VNeeded=[];
for (var i = 0; i < values.length; i++)
{
//var row = "";
var companyCheck="No";
for (var j = 0; j < values[i].length; j++)
{
if (values[i][j])
{
if (i==0)//Need the first row(days) in my new array always
{
VNeeded[i,j]=values[i][j];
Logger.log("VNeeded i=0 (0,3) = "+ VNeeded[0,3]);
Logger.log("VNeeded i=0(1,3)= "+ VNeeded[1,3]);
//row = row + values[i][j];
}
else if (i>0)//From second row onwards check for the company needed
{
if(values[i][0]==Company)
{
companyCheck="Yes";
}
if(companyCheck=="Yes")
{
VNeeded[i,j]=values[i][j];
Logger.log("VNeeded i>0 (0,3) = "+ VNeeded[0,3]);
Logger.log("VNeeded i>0 (1,3)= "+ VNeeded[1,3]);
//row = row + values[i][j];
}
}
}
//row = row + ",";
}
//Logger.log(row);
}
}
我在找这样的东西
VNeeded [] = [10,2.5],[30,4],[40,5],[30,5]
但是我得到的是
VNeeded [] = [2.5,2.5],[4,4],[5,5],[5,5]
检查发生了什么,我使用了Logger.log,它给出了这个结果。
[18-01-28 22:36:30:121 IST] VNeeded i=0 (0,3) = undefined
[18-01-28 22:36:30:122 IST] VNeeded i=0(1,3)= undefined
[18-01-28 22:36:30:123 IST] VNeeded i=0 (0,3) = undefined
[18-01-28 22:36:30:123 IST] VNeeded i=0(1,3)= undefined
[18-01-28 22:36:30:124 IST] VNeeded i=0 (0,3) = undefined
[18-01-28 22:36:30:124 IST] VNeeded i=0(1,3)= undefined
[18-01-28 22:36:30:125 IST] VNeeded i=0 (0,3) = 30
[18-01-28 22:36:30:126 IST] VNeeded i=0(1,3)= 30
[18-01-28 22:36:30:126 IST] VNeeded i=0 (0,3) = 30
[18-01-28 22:36:30:127 IST] VNeeded i=0(1,3)= 30
[18-01-28 22:36:30:127 IST] VNeeded i>0 (0,3) = 30
[18-01-28 22:36:30:128 IST] VNeeded i>0 (1,3)= 30
[18-01-28 22:36:30:128 IST] VNeeded i>0 (0,3) = 30
[18-01-28 22:36:30:129 IST] VNeeded i>0 (1,3)= 30
[18-01-28 22:36:30:130 IST] VNeeded i>0 (0,3) = 30
[18-01-28 22:36:30:130 IST] VNeeded i>0 (1,3)= 30
[18-01-28 22:36:30:131 IST] VNeeded i>0 (0,3) = 5
[18-01-28 22:36:30:132 IST] VNeeded i>0 (1,3)= 5
[18-01-28 22:36:30:132 IST] VNeeded i>0 (0,3) = 5
[18-01-28 22:36:30:133 IST] VNeeded i>0 (1,3)= 5
那我在哪里弄错了?
当我将值设置为VNeeded [1,3]时,相同的值也被设置为另一个维度(VNeeded [0,3])。那怎么可能?
请帮助我摆脱这个问题。
我怎样才能得到理想的结果。我究竟做错了什么
我的测试表的链接是:https://docs.google.com/spreadsheets/d/15hOjIr11Czf1KtrvEgo00pvE0kc7fMfSvRhmrn1tPBM/edit?usp=sharing
请帮忙
Logger.log(JSON.stringify(VNeeded)) gives this result : ["Konabay",2.5,4,5,5]
最佳答案
您对数组位置VNeeded[i,j]
的引用没有给您期望的结果。实际上,您是将位置整数数组传递给VNeeded
,它将解析为最后一个数字。即VNeeded[j]
请改用VNeeded[i][j]
。
VNeeded[i][j]=values[i][j];
然而!
这将在您的代码中引发错误,因为您要在位置'j'处放置值i的“数组”不存在。因此,如果需要,您需要使用数组预填充位置:
VNeeded[i] = VNeeded[i] || [];
VNeeded[i][j] = values[i][j];
然而!
获得所需的结果:
[10,2.5],[30,4],[40,5],[30,5];
在行条件检查的两个部分中都需要将
VNeeded[i][j]
切换为VNeeded[j][i]
。VNeeded[j] = VNeeded[i] || [];
VNeeded[j][i] = values[i][j];
因为您尝试的实际上是对数据透视表的过滤器,所以可以通过执行数据透视表来简化代码,首先,检查公司在列表中的位置并以这种方式返回结果对:
function getData(company) {
var dSheet = SpreadsheetApp.getActiveSpreadsheet();
var aSheet= dSheet.getSheetByName("Data");
var range = aSheet.getDataRange();
var values = range.getValues();
var vNeeded=[];
var pivotedData = []; // Used to carry the transposed data
for (var i = 0, l = data.length; i < l; i += 1) {
for (var j = 0, k = data[i].length; j < k; j += 1) {
pivotedData[j] = pivotedData[j] || []; // allows for entry into preexisting array.
pivotedData[j][i] = data[i][j];
}
}
var companyIndex = pivotedData[0].indexOf(company); // find company in the list
if (companyIndex < 0) return; // There is no matching company, returns undefined
for (var i = 1, l = pivotedData.length; i < l; i += 1) {
vNeeded[i-1] = [pivotedData[i][0], pivotedData[i][companyIndex]]; // [expected,found]
}
return vNeeded;
}