我如何在 javascript 循环中创建批处理到 select x 行然后下一个 x 行直到完成所有行?

how can I create batch in javascript loop to select x Rows then next x Rows until all rows are done?

我有一个 google sheet 和一些数据,我试图将所有单元格数据合并到一个 JSON 变量中,这样我就可以将它传递给 API做某事。

我有这个 javascript 函数,它获取所有数据并将所有内容组合在 JSON 变量中,如下所示:


function combine_val() {
var startRow = 2; // First row of data to process. Starting with 2 to ignore headers
var startColumn = 1; //First Column to process, in case that changes.

var numRows = mysheet.getLastRow(); // Number of rows to process
var numCols = mysheet.getLastColumn(); //Also the number of columns to process, again in case that changes.
var dataRange = mysheet.getRange(startRow, startColumn, numRows, numCols);//Get the full range of data in the sheet dynamically. 
var data = JSON.stringify(dataRange.getValues());//Get the value of the range, AND convert it to a JSON string in one line.

// DO something HERE with "data" to push the JSON string in a controlled batch to API

SpreadsheetApp.getUi().alert(data);  
}

我传递此数据的 API 只需要 JSON 200 行。所以我需要帮助来创建一批 200 个。

这是我目前所做的,需要帮助。

var mybatch = 200;

function combine_val_increment() {
var startRow = 2; // First row of data to process. Starting with 2 to ignore headers
var startColumn = 1; //First Column to process, in case that changes.

var numRows = mysheet.getLastRow(); // Number of rows to process
var numCols = mysheet.getLastColumn(); //Also the number of columns to process, again in case that changes.

for (var i = 0; i < numRows/mybatch; ++i) {

var dataRange = mysheet.getRange(startRow, startColumn, startRow+mybatch, numCols);//Get the full range of data in the sheet dynamically. 
var data = JSON.stringify(dataRange.getValues());//Get the value of the range, AND convert it to a JSON string in one line.

// DO something HERE with "data" to push the JSON string in a controlled batch to API

SpreadsheetApp.getUi().alert(data); 
 startRow = startRow + mybatch;

}


}

基于建议/评论的方法#2

function rowsForAPI2(){
  var batchsize = 2;
  //var batchsize = 200;

  //var ss = SpreadsheetApp.getActiveSheet();
  var ss = SpreadsheetApp.getActive().getSheetByName('Sheet5'); //SHEET NAME
 // var data = ss.getDataRange().getValues(); // 2D array with all of the data in the sheet.


var startRow = 2; // First row of data to process. Skip 1st row of column headers for this test.
var startColumn = 1; //First Column to process, in case that changes.
var numRows = ss.getLastRow(); // Number of rows to process
//var numCols = mysheet.getLastColumn(); //Also the number of columns to process, again in case that changes.
var numCols = 4; //Hardcode for this test
var dataRange = ss.getRange(startRow, startColumn, numRows, numCols);//Get the full range of data in the sheet dynamically. 
var data = dataRange.getValues();//Get the value of the range, AND convert it to a JSON string in one line.  



  var rowCount = ss.getLastRow() - 1; // To know how many rows have data (-1 will ignore the column header)
  var obj = [];
  var temp = 0;
  var results = [];

  Logger.log(rowCount/batchsize)
  for (var i = 0; i < (rowCount/batchsize); i++){  
    for (var j  = temp; j < batchsize*(i+1); j++){
      obj.push(data[j]); // Push row into object.
      temp = j;
      if (temp == rowCount-1) // Got to the end of the data.
        break;
    }
    temp++;
    results.push(JSON.stringify(obj)); // Adds the JSON object to an array
    obj = []; // Clear the array of the 200 rows stored
  }
  return results;
}

function doSomething(){
  var objects = rowsForAPI2();
  var curr;
  for ( var i = 0; i < objects.length; i++){
    curr = objects[i];
    // Do the API thing with curr...

    Logger.log(curr);
  }
}

方法 3 的新要求 -

在这个新用例中,而不是在 JSON.stringify 200 行批量数组中传递数据。我有一个 API 端点采用这种格式的行:

{
  "recipient": {
    "emailAddress": "email_1@domain.com",
    "listName": {
      "path": "testfolder"         
    }
  }

},
{
  "recipient": {
    "emailAddress": "email_2@domain.com",
    "listName": {
       "path": "testfolder" 
    }
  }

},
{
  "recipient": {
    "emailAddress": "email_3@domain.com",
    "listName": {
       "path": "testfolder" 
    }
  }

}

我如何使用下面讨论的相同解决方案和批处理技术来构建上述 ^ 格式的记录,其中电子邮件列表来自 google sheet 中行中的值?有帮助吗?

试试这个:

function rowsForAPI(){
  var ss = SpreadsheetApp.getActiveSheet();
  var data = ss.getDataRange().getValues(); // 2D array with all of the data in the sheet.
  var rowCount = ss.getLastRow(); // To know how many rows have data
  var obj = []; // Array where the row objects will be stored 
  var temp = 0; // A counter of how many rows have been processed. 
  var results = []; // Array where the resulting JSON objects will be stored and returned.

  Logger.log(rowCount/200)
  for (var i = 0; i < (rowCount/200); i++){  
    for (var j  = temp; j < 200*(i+1); j++){
      obj.push(data[j]); // Push row into object.
      temp = j;
      if (temp == rowCount-1) // Got to the end of the data (if there are less than 200 rows in this batch).
        break;
    }
    temp++; // Update row count.
    results.push(JSON.stringify(obj)); // Adds the JSON object to an array
    obj = []; // Clear the array of the 200 rows stored before the next loop starts.
  }
  return results;
}

function doSomething(){
  var objects = rowsForAPI();
  var curr;
  for ( var i = 0; i < objects.length; i++){ // Go through each batch
    curr = objects[i]; // Current batch.
    // Do the API thing with curr...
  }
}

此方法将 return 一个包含 JSON 个对象的数组,其中包含来自 Sheet 的 200 行批次,如果它到达数据的末尾,它也会停止sheet。