我有一张表,其中第一行是天数。第二和第三行是这些天数内不同公司的日常生产。它是这样的。
我的要求是在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]
答案 0 :(得分:2)
由于comma operator,JavaScript VNeeded[i,j]
评估为VNeeded[j]
。
请改用VNeeded[i][j]
。
我必须将代码粘贴到https://www.typescriptlang.org/play/以实现此问题。
答案 1 :(得分:2)
您对数组位置VNeeded[i,j]
的引用并未提供您期望的结果。实际上,您将位置整数数组传递给VNeeded
,这将解析为最后一个数字。即VNeeded[j]
请改用VNeeded[i][j]
。
VNeeded[i][j]=values[i][j];
<强>无论其!强>
这将在您的代码中引发错误,因为&#34;数组&#34;在位置i,你试图把价值放在位置&#39; j&#39;不存在。因此,如果需要,您需要使用数组预填充该位置:
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;
}
答案 2 :(得分:0)
您可以使用此函数展平数组:
function flattenArray(array){
var flatArray = []
for (var i = 0; i < array.length; i++) {
flatArray.push(array[i][0])
}
return flatArray
}