使用 node.js 遍历智能表中的行并根据列中的值缩进

Iterate over rows in smartsheet and indent based on value in column using node.js

我的 Smartsheet 和 node.js 世界正在扩大 :-),但现在我不知道如何处理。我有一个包含大约 1000 行的平面结构化智能表,我需要使用 node.js 缩进选定的行。该系统(对于本例)分为三层;章、节和段落。我有一个列(称为标记),它按名称标识图层(参见下面的示例)。

我需要做的是缩进章节下面的小节,以及小节下面的段落。请注意,一个章节可以包含多个部分。 (另请注意,示例仅此而已。我不能给出真实数据,抱歉。)

[2021 年 8 月 1 日更新]

我快到了(非常接近)...

我认为在优化和记录之前我需要做的最后一件事就是让缩进位起作用。我的代码如下...

//Initialise Var
var sheetno=1234567890123456;  //this is the sheet ID 
var colcount=0; 
var chap,sect,rowno;
var rowdetail="nothing";
var row_ids='';
var rowList=[];
var row_ids='';
var rowList=[];
var rowdetail='';
var guiderow='';
var sectrow='';
var optionupdate='';
var newsectline='';
var newguideline='';

// Set queryParameters for `include` and pagination
var options1 = {
      id: sheetno,
  queryParameters: {
      pageSize: 1000,
    includeAll: true
  }
};

    // Load the sheet we are interested in
async function indent() {
    console.log("Starting process.  Please wait...");
    smartsheet.sheets.getSheet(options1)
    .then(async function(sheetInfo) {
        var rowCount=sheetInfo.totalRowCount;  //variable to hold the number of rows
        var totcolcount=sheetInfo.columns.length;  //variable to hold the number of columns
        var colcount=totcolcount-1;  //This accounts for arrays starting at the zero position.
        console.log("row count is ", rowCount, ". Column count is ", totcolcount)
        // iterate through rows in the sheet and make sure they are in the right order
        for (var i=0; i < rowCount; i++) {
            rowdetail=sheetInfo.rows[i].id;
            var rowloc=sheetInfo.rows[i].rowNumber;
            rowList[rowloc]=rowdetail;
        }
        // iterate through5 rows in the sheet
        for (var j=1; j < rowCount; j++) {  //change from 16 to rowCount when running in production
            var options2 = {
                sheetId: Number(sheetno),
                rowId: Number(rowList[j])
            };
            await procrow(options2,j,colcount);
            await sleep(3000);
        }
    })
    .catch(function(error) {
        console.log(error);
    })
}

function procrow(options2,j,colcount) {//Return your promise and let it be controlled outside of function
    return new Promise((resolve, reject) => {
        try {
            smartsheet.sheets.getRow(options2)
            .then(async function(row) {
                var rowid = row.id;
                console.log("j=", j, ", colcount=", colcount, ", Rowid = ", rowid, ", Guideline rowid=", guiderow, ", Section rowid=", sectrow, ", Newguideline=", newguideline, ", Newsectline=", newsectline );
                if (row.cells[colcount].value == "guideline") {
                    console.log("Found guideline");
                    if (newguideline==0) {
                        guiderow=rowid;
                        newguideline=1;
                        newsectline=0;
                    }
                    else {
                        // close off the general rows
                        optionupdate= {
                            sheetId: sheetno,
                            "rowId": rowid,
                            body: [{"parentId": sectrow, "toBottom": true}]
                            };
                        // close off section
                        await updateRow(optionupdate);
                        optionupdate= {
                            sheetId: sheetno,
                            "rowId": rowid,
                            body: [{"parentId": sectrow, "toBottom": true}]
                            };
                        await updateRow(optionupdate);
                        guiderow=rowid;
                    }
                }
                else if (row.cells[colcount].value == "section") {
                    console.log("Found section");
                    if (newsectline!=1) {
                        sectrow=rowid;
                        newsectline=1;
                        var rowstuff=[{
                            "Id": rowid,
                            "parentId": guiderow
                            }];
                        optionupdate= {
                            sheetId: sheetno,
                            row: rowstuff
                        };
                        console.log("optionupdate for section =", optionupdate);
                        await updateRow(optionupdate);

                    }
                    else {
                        // close off the general rows
                        var rowstuff=[{
                            "Id": rowid,
                            "parentId": sectrow,
                            "toBottom": true
                        }]
                            optionupdate = {
                            sheetId: sheetno,
                            rowstuff
                        };
                        console.log("optionupdate for section - closing off general rows =", optionupdate);
                        await updateRow(optionupdate);
                        newsectline=0;
                    }
                }
                else if (row.cells[colcount].value == "") {
                    optionupdate= {
                        sheetId: sheetno,
                        "rowId": rowid,
                        body: [{parentId: sectrow}]
                    };
                    await updateRow(optionupdate);
                }
                resolve();
            })
            .catch(function(error) {
                console.log(error);
            });
        } catch (err) {
            reject(err);
        };
    });
}

// DUMMY SLEEP FUNCTION
var sleep = function (ms) {
    let now = Date.now(), end = now + ms;
    while (now < end) { now = Date.now(); }
};

 function updateRow(optionupdate) {//Return your promise and let it be controlled outside of function
    return new Promise((resolve, reject) => {
        try {
            smartsheet.sheets.updateRow(optionupdate);
            resolve();
        } catch (err) {
            reject(err);
        };
    })
 }


indent()

所以当我 运行 我得到...

Starting process.  Please wait...
row count is  863 . Column count is  51
j= 1 , colcount= 50 , Rowid =  1139670832965508 , Guideline rowid=  , Section rowid=  , Newguideline=  , Newsectline=
Found guideline
j= 2 , colcount= 50 , Rowid =  5643270460336004 , Guideline rowid= 1139670832965508 , Section rowid=  , Newguideline= 1 , Newsectline= 0
Found section
optionupdate for section = {
  sheetId: 6458324490184580,
  row: [ { Id: 5643270460336004, parentId: 1139670832965508 } ]
}
[Smartsheet] 2021-08-01T10:01:53.682Z[  ERROR] Request failed after 0 retries
[Smartsheet] 2021-08-01T10:01:53.683Z[  ERROR] PUT https://api.smartsheet.com/2.0/sheets/6458324490184580/rows
[Smartsheet] 2021-08-01T10:01:53.684Z[  ERROR] Response: Failure (HTTP 400)
        Error Code: 1008 - Unable to parse request. The following error occurred: Request body must be either a JSON object or JSON array.
        Ref ID: bqto35luphre
Unhandled rejection (<{"statusCode":400,"errorCode":1008,"me...>, no stack trace)

不确定我做错了什么,但显然这与部分代码有关。

我需要做的是缩进章节下面的小节,以及小节下面的段落。请注意,一个章节可以包含多个部分。 (另请注意,示例仅此而已。我不能给出真实数据,抱歉。)

因为我在让它工作时遇到了问题,所以我认为一旦我成功了它会对其他人有用 :-)。尚待优化和记录。

//Initialise Var
var sheetno=1234567890123456;  //this is the sheet ID 
var colcount=0; 
var chap,sect,rowno;
var rowdetail="nothing";
var row_ids='';
var rowList=[];
var row_ids='';
var rowList=[];
var rowdetail='';
var guiderow='';
var sectrow='';
var optionupdate='';
var newsectline='';
var newguideline='';

// Set queryParameters for `include` and pagination
var options1 = {
      id: sheetno,
  queryParameters: {
      pageSize: 1000,
    includeAll: true
  }
};

    // Load the sheet we are interested in
async function indent() {
    console.log("Starting process.  Please wait!  This takes AAAAAges...");
    smartsheet.sheets.getSheet(options1)
    .then(async function(sheetInfo) {
        var rowCount=sheetInfo.totalRowCount;  //variable to hold the number of rows
        var totcolcount=sheetInfo.columns.length;  //variable to hold the number of columns
        var colcount=totcolcount-1;  //This accounts for arrays starting at the zero position.
        console.log("row count is ", rowCount, ". Column count is ", totcolcount)
//      console.log(sheetInfo);
        // iterate through rows in the sheet and make sure they are in the right order
        for (var i=0; i < rowCount+1; i++) {
            rowdetail=sheetInfo.rows[i].id;
            var rowloc=sheetInfo.rows[i].rowNumber;
            rowList[rowloc]=rowdetail;
        }
        // iterate through rows in the sheet
        for (var j=1; j < rowCount+1; j++) {  //change from 16 to rowCount when running in production
            var options2 = {
                sheetId: Number(sheetno),
                rowId: Number(rowList[j])
            };
            await procrow(options2,j,colcount);
            await sleep(3000);
        }
    })
    .catch(function(error) {
        console.log(error);
    })
}

function procrow(options2,j,colcount) {//Return your promise and let it be controlled outside of function
    return new Promise((resolve, reject) => {
        try {
            smartsheet.sheets.getRow(options2)
            .then(async function(row) {
                var rowid = row.id;
                console.log("j=", j, ", colcount=", colcount, ", Rowid = ", rowid, ", Newguideline=", newguideline, ", Newsectline=", newsectline, "Indent Level = ", indentlevel );
                if (row.cells[colcount].value == "guideline") {  // starting chapter level
                    console.log("Found guideline");
                    if (newguideline=='') {
                        newguideline=1;
                    }
                    else { // found a chapter heading below the start. need to remove ANY indenting knowing that the last item will have been a control and therefore we will need to come back TWO levels (outdent twice).
                        indentlevel=0;
//                      console.log("end of chapter 1 indent level = ", indentlevel);
                        optionupdate= {
                            sheetId: sheetno,
                            body :  [{"id": rowid, "outdent": 0}]
                        };
                    }
                }
                else if (row.cells[colcount].value == "section") {
                    console.log("Found section");
                        indentlevel=1;
//                      console.log("section indent level = ", indentlevel);
                        optionupdate= {
                            sheetId: sheetno,
                            body :  [{"id": rowid, "indent": 1}]
                        };
                        await updateRow(optionupdate);
                    
                }
                else {
                    indentlevel=2;
                    optionupdate= {
                        sheetId: sheetno,
                        body :  [{"id": rowid, "indent": 2}]
                    };
                    await updateRow(optionupdate);
                }
                resolve();
            })
            .catch(function(error) {
                console.log(error);
            });
        } catch (err) {
            reject(err);
        };
    });
}

// DUMMY SLEEP FUNCTION
var sleep = function (ms) {
    let now = Date.now(), end = now + ms;
    while (now < end) { now = Date.now(); }
};

 function updateRow(optionupdate) {//Return your promise and let it be controlled outside of function
    return new Promise((resolve, reject) => {
        try {
            smartsheet.sheets.updateRow(optionupdate);
            sleep(4000);
            resolve();
        } catch (err) {
            reject(err);
        };
    })
 }


indent()