我正在使用Google工作表和脚本来编写从表中查找数据的函数,并将记录作为哈希返回。可能有更好的方法,但是现在,我试图返回一组记录哈希值,其中键是字段名称。
我遇到的问题是从阵列中的单个记录哈希调用密钥。它只是回归' undefined'。这是我编写的测试代码部分,用于查看我的应用程序代码,我遇到了问题:
function hashArrayTest() {
var workbookKey = "I blanked this out"
var tableName = "AttributeChildParentJoin";
var headerRow = 1;
var i;
var attrsJoinObj = new dbEntitySource(workbookKey, tableName, headerRow);
var OutputHashArray = attrsJoinObj.returnHashItems("Parent Attribute", "Child Attribute", "AESTHETICS");
Logger.log("This is the resulting output array of hashes: ");
for(i=0; i<OutputHashArray.length; i++){
Logger.log("-------------" + i + "-------------");
Logger.log(OutputHashArray[i]);
}
Logger.log("Here are the Parent Attributes from each hash: ");
for(i=0; i<OutputHashArray.length; i++){
Logger.log("-------------" + i + "-------------");
Logger.log(OutputHashArray[i]['Parent Attribute']);
}
}
这里是源数据表:
以下是代码输出的日志:
[-] This is the resulting output array of hashes:
[-] -------------0-------------
[-] {'Date Added'='', 'Child Attribute'='Color', 'Parent Attribute'='AESTHETICS', 'Added by'='', 'ID'='21'}
[-] -------------1-------------
[-] {'Date Added'='', 'Child Attribute'='Finish', 'Parent Attribute'='AESTHETICS', 'Added by'='', 'ID'='22'}
[-] -------------2-------------
[-] {'Date Added'='', 'Child Attribute'='Material', 'Parent Attribute'='AESTHETICS', 'Added by'='', 'ID'='23'}
[-] -------------3-------------
[-] {'Date Added'='', 'Child Attribute'='VBL Styling Adherence', 'Parent Attribute'='AESTHETICS', 'Added by'='', 'ID'='24'}
[-] Here are the Parent Attributes from each hash:
[-] -------------0-------------
[-] undefined
[-] -------------1-------------
[-] undefined
[-] -------------2-------------
[-] undefined
[-] -------------3-------------
[-] undefined
在这个输出中,看起来好像我已经成功创建了一个哈希数组,尽管我无法从每个哈希中调用[&#39; Parent Attribute&#39;]键。
我写了一个非常简单的函数来测试哈希数组的问题,但这次我成功了。 =&GT;
这是代码:
function rawHashTest(){
var recordHash1 = {};
recordHash1['car'] = 45;
recordHash1['chicken'] = "ten";
var recordHash2 = {};
recordHash2['red'] = 30;
recordHash2['sticks'] = "blue";
Logger.log(recordHash1);
Logger.log(recordHash1['car']);
var hashArray = new Array();
hashArray = [recordHash1, recordHash2];
Logger.log(hashArray);
Logger.log(hashArray[0]);
Logger.log(hashArray[0]["car"]);
}
这里是日志:
[-] {chicken=ten, car=45.0}
[-] 45.0
[-] [{chicken=ten, car=45.0}, {red=30.0, sticks=blue}]
[-] {chicken=ten, car=45.0}
[-] 45.0
我无法弄清楚为什么我能够在我的简单测试代码中从数组中的一个哈希中调用哈希键值,但在我测试我的应用程序代码时却不能。我已经尝试了所有不同的方式,并且我检查了前导和尾随空格。我在这里错过了什么?
谢谢你, 尼古拉斯金凯德
答案 0 :(得分:0)
谢谢大家的问题探测。我检查了我的returnHashItems方法,发现了问题。
我的dbEntitySource.returnHashItems方法使用另一种方法来构建每个单独的哈希。这是包含原始行和更新行的代码:
this.returnHash = function returnHash(recordIndex){
var recordHash = {};
if (recordIndex !== -1){
for (i=0; i<this.headerArray.length;i++){
// This is the line that was giving me the issue:
//recordHash["'"+ this.headerArray[i] +"'"] = "'" + this.dataArray[recordIndex][i] + "'";
// This is the line that fixed the problem:
recordHash[this.headerArray[i]] = this.dataArray[recordIndex][i];
}
}
return recordHash;
}
回顾它,看起来我原本以为我必须将它括在引号中,因为这是直接指定字符串的方式,例如:
var recordHash1 = {};
recordHash1['car'] = 45;
recordHash1['chicken'] = "ten";
但是,由于headerArray和dataArray中的值已经是字符串,因此这是多余的,也会导致问题。什么类型的问题,但我还不确定。
谢谢cssimsek的调试提示。我在修复之前和之后检查了OutputHashArray [0]的类型:
function hashArrayTest() {
var workbookKey = "1uOHlrW8pSACDg9dNUzMFKgLi8fDBT4Wsqc5KadkoCXk";
var tableName = "AttributeChildParentJoin";
var headerRow = 1;
var i;
var attrsJoinObj = new dbEntitySource(workbookKey, tableName, headerRow);
var OutputHashArray = attrsJoinObj.returnHashItems("Parent Attribute", "Child Attribute", "AESTHETICS");
Logger.log("Here is the data type for the OutputHashArray:");
Logger.log(typeof OutputHashArray);
Logger.log("Here is the data type for the OutputHashArray[0]:");
Logger.log(typeof OutputHashArray[0]);
Logger.log("This is the resulting output array of hashes: ");
for(i=0; i<OutputHashArray.length; i++){
Logger.log("-------------" + i + "-------------");
Logger.log(OutputHashArray[i]);
}
Logger.log("Here are the Parent Attributes from each hash: ");
for(i=0; i<OutputHashArray.length; i++){
Logger.log("-------------" + i + "-------------");
Logger.log(OutputHashArray[i]['Parent Attribute']);
}
}
修复前的输出:
[ - ]这是OutputHashArray的数据类型:
[ - ]对象
[ - ]这是OutputHashArray [0]的数据类型:
[ - ]对象
[ - ]这是哈希的结果输出数组:
[ - ] ------------- 0 -------------
[ - ] {&#39;添加日期&#39; =&#39;&#39;,&#39;儿童属性&#39; =&#39;颜色&#39;,&#39;父属性&#39; ; =&#39; AESTHETICS&#39;,&#39; =&#39;&#39;&#39; ID&#39; =&#39; 21&#39;}
[ - ] ------------- 1 -------------
[ - ] {&#39;添加日期&#39; =&#39;&#39;,&#39;儿童属性&#39; =&#39;完成&#39;,&#39;父属性&#39; ; =&#39; AESTHETICS&#39;,&#39; =&#39;&#39;&#39; ID&#39; =&#39; 22&#39;}
[ - ] ------------- 2 -------------
[ - ] {&#39;添加日期&#39; =&#39;&#39;,&#39;儿童属性&#39; =&#39;材料&#39;,&#39;父属性&#39; ; =&#39; AESTHETICS&#39;,&#39; =&#39;&#39;&#39; ID&#39; =&#39; 23&#39;}
[ - ] ------------- 3 -------------
[ - ] {&#39;添加日期&#39; =&#39;&#39;,&#39;儿童属性&#39; =&#39; VBL样式遵守&#39;,&#39;父属性& #39; =&#39; AESTHETICS&#39;,&#39;添加&#39; =&#39;&#39;,&#39; ID&#39; =&#39; 24&#39;}
[ - ]以下是每个哈希的父属性:
[ - ] ------------- 0 -------------
[ - ] undefined
[ - ] ------------- 1 -------------
[ - ] undefined
[ - ] ------------- 2 -------------
[ - ] undefined
[ - ] ------------- 3 -------------
[ - ] undefined
修复后的输出:
[ - ]这是OutputHashArray的数据类型:
[ - ]对象
[ - ]这是OutputHashArray [0]的数据类型:
[ - ]对象
[ - ]这是哈希的结果输出数组:
[ - ] ------------- 0 -------------
[ - ] {添加日期=,子属性=颜色,ID = 21.0,父属性=美学,添加=}
[ - ] ------------- 1 -------------
[ - ] {添加日期=,子属性=完成,ID = 22.0,父属性=美学,添加=}
[ - ] ------------- 2 -------------
[ - ] {添加日期=,子属性=材料,ID = 23.0,父属性=美学,添加=}
[ - ] ------------- 3 -------------
[ - ] {添加日期=,子属性= VBL样式依从性,ID = 24.0,父属性=美学,添加=}
[ - ]以下是每个哈希的父属性:
[ - ] ------------- 0 -------------
[ - ] AESTHETICS
[ - ] ------------- 1 -------------
[ - ] AESTHETICS
[ - ] ------------- 2 -------------
[ - ] AESTHETICS
[ - ] ------------- 3 -------------
[ - ] AESTHETICS
看起来问题不在于我的hashArray的数据类型,而在于我如何指定哈希键。谢谢大家的调试技巧,真的很有帮助!