0
我一直在這工作了幾天,似乎正在取得進展,但我無法弄清楚錯誤。目標:從本地服務器進行查詢,將結果存儲到可發送到Google表格batchUpdate的「值」屬性的數組中。JS - 數組對象到Google表批量更新
我有一個時間與整個asynchronus事情地獄,但我想我已經有了承諾一個手柄,現在,它的工作有點...
到目前爲止,我的代碼如下所示:
function begin(auth) {
var theArray = [];
let getResults = (passedQuery) => {
return new Promise((resolve, reject) => {
client.connect();
var sql = client.query(passedQuery);
sql.on('row', (row, result) => {
result.addRow(row);
});
sql.on('end', (result) => {
resolve(result.rows); // Array.from(result.rows)??
client.end();
});
});
}
// may not even need this...
let convertArray = (sqlObj) => {
return new Promise((resolve, reject) => {
theArray = Object.keys(sqlObj).map((k) => {
return [sqlObj[k]];
});
console.log(theArray);
// sqlObj.forEach((element) => {
// theArray.push(element);
// })
// theArray = sqlObj.reduce((result, item) => {
// var key = Object.keys(item)[0]; // first property
// result[key] = item[key];
// return result;
// }, []);
// console.log(theArray);
resolve(theArray);
// while (sqlObj.length) {
// theArray.push(sqlObj.splice(0, sqlObj.length));
// console.log(sqlObj.length);
// }
// console.log('convertArray should be done and now includes: \n'+JSON.stringify(theArray));
// resolve(theArray);
});
}
let updateSheets = (myArray) => {
return new Promise((resolve, reject) => {
var update_range = "Sheet1!A:Z";
var valueInputOption = "RAW";
var sheets = google.sheets('v4');
var options = {
auth: auth,
spreadsheetId: spreadsheetId,
resource: {
valueInputOption: valueInputOption,
data: [
{
range: update_range,
majorDimension: "COLUMNS",
// values: [[JSON.stringify(myArray,null,' ')]] // stringify and it works...in 1 cell...
values: myArray // this should be values only! NO KEYS/COLUMN HEADERS
}
]
}
}
sheets.spreadsheets.values.batchUpdate(options, (err, res) => {
if (err) {
reject(err);
} else {
console.log(res);
resolve(res);
}
});
});
}
getResults('SELECT item_number,item_descrip1 FROM item WHERE item_number LIKE \'23-%\' LIMIT 10')
.then((sqlObj) => {
console.log('Converting object to array...hopefully...');
return convertArray(sqlObj);
})
.then((convertedObj) => {
console.log('Updating sheets...');
return updateSheets(convertedObj);
})
.catch((error) => {
console.log('Something went wrong...\n%s', error);
})
}
您可以通過我的意見,我一直在嘗試各種方式來獲取對象進入一個[X] d陣列看到(基於查詢返回的列數)
就這樣,我的結果...
Info: Start process (1:30:25 PM)
Converting object to array...hopefully...
[ [ anonymous {
item_number: '23-1240',
item_descrip1: 'Dev1 TX SDI with Lemo' } ],
[ anonymous {
item_number: '23-1241',
item_descrip1: 'Dev1 RX SDI with Lemo' } ],
[ anonymous { item_number: '23-0042-B', item_descrip1: 'Dev2 Assy' } ],
[ anonymous { item_number: '23-0036-B', item_descrip1: 'Dev3 Assy' } ],
[ anonymous { item_number: '23-0026-B', item_descrip1: 'Dev4 Ship Kit' } ],
[ anonymous {
item_number: '23-0055',
item_descrip1: 'Dev5 TX SDI Unicast Assy' } ],
[ anonymous { item_number: '23-0118', item_descrip1: 'Dev6 AB Mount' } ],
[ anonymous { item_number: '23-0119', item_descrip1: 'Dev7 V Mount' } ],
[ anonymous {
item_number: '23-0203',
item_descrip1: 'Dev8 RX HDMI' } ],
[ anonymous {
item_number: '23-0206',
item_descrip1: 'Dev9 TX HDMI' } ] ]
Updating sheets...
Something went wrong...
Error: Invalid data[0]: Invalid values[0][0]: struct_value {
fields {
key: "item_number"
value {
string_value: "23-1240"
}
}
fields {
key: "item_descrip1"
value {
string_value: "Dev1 TX SDI with Lemo"
}
}
}
Info: End process (1:30:26 PM)
我相當有信心數組中的「鍵」不被batchUpdate調用所理解,但我的嘗試正在刪除,但仍給我類似的結果。