首页 > 解决方案 > 使用 for 循环增量打印数组项

问题描述

我试图让一行注释掉的块在 rowEcho 上重复,具体取决于batchLength。用注释掉的“数组”替换整个函数按预期工作。这一切都发生在一个 data:[] 对象中,我试图将这些值推送到谷歌表格,以更新每一行中的一个单元格,以便在响应数组中找到尽可能多的行。

感谢您的回复,但这些选项都没有按预期工作。我发布了更完整的代码以提供更好的范围,如您所见,我正在使用带有 sheet API 的 preadsheets.values.batchUpdate 请求来尝试推送新数组,但是使用 batchUpdate 方法必须按顺序知道整个新数据范围要覆盖,似乎没有办法更新列中的所有单元格,除非您有范围结束,因此我的下面尝试使用新数据生成每个数组,只要工作表中有行。脏我知道。。

function changeSentValue(auth) {   
      var x = 0;
      for (var x = 0; x < rows.length; x++) {

        // put update spreadt sheet here
        console.log('updating to sent items..');

        const sheets = google.sheets({ version: 'v4', auth });

         sheets.spreadsheets.values.batchUpdate(request, function(err, response) {
            //var rows = response.data.values;

            if (err) {
              console.error(err);
              return;
            }
    ///here

          });

          var request = {
            spreadsheetId: 'xxxxxxxxxxxxxxxxxxxxx',
            resource: {
              valueInputOption: 'RAW',
              includeValuesInResponse: true,
              responseValueRenderOption: "UNFORMATTED_VALUE",

              // The new values to apply to the spreadsheet.
              data: [
                {
                  range: 'Sheet1!A2',
                  majorDimension: "ROWS",
               // values: [[5],[2],[5]]
               values: [

             function getLength ()  {
              var batchRows = response.data.values;
              var batchLength = 13/* batchRows.length */;
              var rowEcho = '[null,null,null,null,null,null,null,null,null,null,\"not\"],'; //col 11
            var y 
              for (y = 0; y < batchLength; y ++) {


                //write(rowEcho);
                 //console.log(rowEcho(y));
                 //these both dont work as expected
                }

                }
                /* [null,null,null,null,null,null,null,null,null,null,"notSent"],
                [null,null,null,null,null,null,null,null,null,null,"notSent"],
                [null,null,null,null,null,null,null,null,null,null,"notSent"],
                [null,null,null,null,null,null,null,null,null,null,"notSent"],
                [null,null,null,null,null,null,null,null,null,null,"notSent"],
                [null,null,null,null,null,null,null,null,null,null,"notSent"],
                [null,null,null,null,null,null,null,null,null,null,"notSent"],
                [null,null,null,null,null,null,null,null,null,null,"notSent"],
                [null,null,null,null,null,null,null,null,null,null,"notSent"],
                [null,null,null,null,null,null,null,null,null,null,"notSent"],
                [null,null,null,null,null,null,null,null,null,null,"notSent"],
                */
              ]


            }
          ],  // TODO: Update placeholder value.

          // TODO: Add desired properties to the request body.
        },

          auth: auth,
        }


      }


      }

标签: javascriptnode.jsfunctionfor-loopgoogle-sheets

解决方案


function getLength() {
var batchLength = 8/* batchRows.length */;
var rowEcho = '[null,null,null,null,null,null,null,null,null,null,\"not\"],'; //col 11
var y
for (y = 0; y < batchLength; y++) {
//You forgot to print it
    console.log(rowEcho);
}
}

推荐阅读