无法将值数组更新为 Google sheet 中的单个范围

Cant update an array of values to a single range in Google sheet

我已经成功地使用了GoogleSheets API的"spreadsheets#get"请求指定范围,并对数组中的每一项进行了一些计算,将它们存储到一个新数组中。

现在我需要用我计算的数组项更新 Google Sheet 中的另一个范围。

我已成功从单个单元格中获取数据,对该值进行计算,并将新数据写入另一个单元格。但是当我处理一系列单元格时,出现错误

"Requested writing within range [BILLING!D2:D], but tried writing to column [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' } ] }

问题通过将输出的主要维度更改为列或通过将输出数组更改为主要维度=ROW 来解决。 之前我的数组就像 array = [1,2,3,4] 现在变成 array = [[1],[2],[3],[3]].

对于输出,如果电子表格数据是:A1=1,B1=2,A2=3,B2=4,那么请求range=A1:B2,majorDimension=ROWS会return [[1 ,2],[3,4]],而请求 range=A1:B2,majorDimension=COLUMNS 将 return [[1,3],[2,4]].

对于输入,range=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.