我已经在指定范围内成功使用Google Sheets API的“ spreadsheets#get”请求,并对数组中的每个项目进行了一些计算,并将它们存储到新数组中。
现在,我需要使用我计算出的数组项目更新Google表格中的另一个范围。
我已经成功地从单个单元格中获取了数据,对该值进行了计算,然后将新数据写入了另一个单元格中。但是当我在一系列单元格上工作时,我得到了错误
“请求在[BILLING!D2:D]范围内写入,但尝试写入[E]列”
其中[BILLING! D2:D]
是我要更新的范围。
这是我的一些代码
读取数据
const sheets = google.sheets({version: 'v4', auth});
let ranges = ['BILLING!A2:A']
sheets.spreadsheets.values.get({
spreadsheetId: '1F1ed24fgZIQChueq1AGrKIYRXKrX-vK3RAIYKd5lVrE',
range: ranges,
}, (err, res) => {
let sma_array = [];
async(res.data.values, (value, callback) => {
let id = value[0];
let url = 'https://www.quandl.com/api/v3/datasets/BSE/BOM' + id + '?limit=57&api_key=' + MY_API_KEY;
reqs.requestFun(url, id).then((result, err) => {
sma_array.push(result)
console.log(id,result)
setTimeout(function () { callback(); }, 500);
})
}, function (err) {
writing(sma_array)
});
})
来自A2:A的输入数组:-
[ [ '532424' ],
[ '500003' ],
[ '524634' ],
[ '531978' ],
[ '500493' ],
[ '532321' ],
[ '530307' ],
[ '532622' ],
[ '500300' ],
[ '530001' ],
[ '532457' ],
[ '524494' ],
[ '524731' ],
[ '500233' ],
[ '532714' ],
[ '524500' ],
[ '539229' ],
[ '524084' ],
[ '538019' ],
[ '533179' ],
[ '523539' ],
[ '532805' ],
[ '500367' ],
[ '531431' ],
[ '503811' ],
[ '532343' ],
[ '504212' ],
[ '507880' ],
[ '532953' ],
[ '540743' ] ]
文字数据
let writing = function (sma_array) {
console.log(sma_array)
let values = [
sma_array
]
const resource = {
values: values
}
let ranges = ['BILLING!D2:D']
sheets.spreadsheets.values.update({
spreadsheetId: '1F1ed24fgZIQChueq1AGrKIYRXKrX-vK3RAIYKd5lVrE',
range: ranges,
valueInputOption: 'RAW',
resource: resource,
}, (err, result) => {
if (err) {
// Handle error
console.log(err);
} else {
console.log('%d cells updated.', result.updatedCells);
}
});
}
需要更新D2:D的数组:-
[ [ 702.8052631578946,
198.67719298245612,
105.9236842105263,
1137.5780701754388,
496.70263157894743,
329.87456140350884,
78.04912280701757,
115.24473684210525,
784.7464912280702,
490.9464912280701,
56.51228070175439,
841.3201754385965,
498.77192982456154,
554.2035087719298,
268.8666666666667,
199.00438596491227,
37.27543859649123,
2592.591228070176,
27.73684210526315,
626.8912280701755,
205.12368421052628,
85.49035087719298,
40.75175438596491,
391.38947368421043,
369.10789473684207,
488.36315789473656,
227.23596491228068,
458.780701754386,
206.2780701754385,
208.4875856548958 ] ]
错误
errors:
[ { message:
'Requested writing within range [BILLING!D2:D3], but tried writing to column [E]',
domain: 'global',
reason: 'badRequest' } ] }
答案 0 :(得分:0)
通过将输出的majorDimension更改为列或通过更改输出数组(如majorDimension = ROW)来解决该问题。 在我的数组像array = [1,2,3,4]之前,现在变成了array = [[1],[2],[3],[3]]。
对于输出,如果电子表格数据为:A1 = 1,B1 = 2,A2 = 3,B2 = 4,则请求范围= A1:B2,majorDimension = ROWS将返回[[1,2],[3 ,4]],而请求range = A1:B2,majorDimension = COLUMNS将返回[[1,3],[2,4]]。
对于输入,范围为A1:B2,majorDimension = ROWS,则[[1,2],[3,4]]将设置A1 = 1,B1 = 2,A2 = 3,B2 = 4。如果range = A1:B2,majorDimension = COLUMNS,则[[1,2],[3,4]]将设置A1 = 1,B1 = 3,A2 = 2,B2 = 4。