gpt4 book ai didi

javascript - 如何在 javascript 循环中创建批处理以选择 x 行,然后选择下 x 行,直到完成所有行?

转载 作者:行者123 更新时间:2023-12-02 23:00:13 24 4
gpt4 key购买 nike

我有一个包含一些数据的 Google 工作表,我正在尝试将所有单元格数据合并到一个 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 仅接受包含 200 行的 JSON。所以我需要帮助来创建一批 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 的新要求 -

在这个新用例中,不是在 200 行批量的 JSON.stringify 数组中传递数据。我有一个 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 表格中的行中的值?有什么帮助吗?

最佳答案

试试这个:

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...
}
}

此方法将返回一个 JSON 对象数组,其中包含工作表中 200 行的批处理,如果到达工作表中数据的末尾,它也会停止。

关于javascript - 如何在 javascript 循环中创建批处理以选择 x 行,然后选择下 x 行,直到完成所有行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57822236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com