0

经历了令人沮丧的一天左右:-(

我会很高兴被告知我快疯了(如果你能指出我的方式的错误)。

我正在尝试循环浏览智能表(只有 900 行)并根据列的内容自动缩进(称为标记 - 在本例中为第 51 列)。如果标记是指南,那么它是顶级父级,如果它是部分,那么它是指南的孩子,如果它是空白,那么它是部分的孩子。指南可以有多个部分,部分可以有多个(空白)子项。

我的代码仍然处于萌芽状态,远非优雅 :-( 我只是想让它工作!!!一旦它运行,我会回来整理它。你可以看到我已经尝试了一些不同的东西。

首先是代码(然后我得到的错误低于该代码)。

//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)
//      console.log(sheetInfo);
        // 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 through 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= Number(rowid);
                        newguideline=1;
                        newsectline=0;
                    }
                    else {
                        // close off the general rows
                        optionupdate= {
                            sheetId: sheetno,
                            id: Number(rowid),
                            row: JSON.stringify([{parentId: sectrow, toBottom: true}])
                            };
                        // close off section
                        await updateRow(optionupdate);
                        var data=JSON.stringify([{parentId: sectrow, toBottom: true}]);
                        optionupdate= {
                            sheetId: sheetno,
                            id: Number(rowid),
                            row: data
                            };
                        await updateRow(optionupdate);
                        guiderow=rowid;
                    }
                }
                else if (row.cells[colcount].value == "section") {
                    console.log("Found section");
                    if (newsectline!=1) {
                        sectrow=rowid;
                        newsectline=1;
                        var data = JSON.stringify([
                                {
                                    indent: 1
                                }
                            ]);
                        optionupdate= {
                            sheetId: sheetno,
                            id: Number(rowid),
                            row: data
                        };
                        console.log("optionupdate for section =", optionupdate);
                        await updateRow(optionupdate);
                    }
                    else {
                        // close off the general rows
                            optionupdate = {
                            sheetId: sheetno,
                            id: Number(rowid),
                            row: JSON.stringify([{parentId: sectrow, toBottom: true}])
                        };
                        console.log("optionupdate for section - closing off general rows =", optionupdate);
                        await updateRow(optionupdate);
                        newsectline=0;
                    }
                }
                else if (row.cells[colcount].value == "") {
                    optionupdate= {
                        sheetId: sheetno,
                        id: Number(rowid),
                        row: JSON.stringify([{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...
[Smartsheet] 2021-08-02T05:26:31.655Z[   INFO] GET https://api.smartsheet.com/2.0/sheets/6458324490184580?pageSize=1000&includeAll=true
[Smartsheet] 2021-08-02T05:26:33.561Z[   INFO] Response: Success (HTTP 200)
row count is  863 . Column count is  51
[Smartsheet] 2021-08-02T05:26:33.623Z[   INFO] GET https://api.smartsheet.com/2.0/sheets/6458324490184580/rows/1139670832965508
[Smartsheet] 2021-08-02T05:26:34.431Z[   INFO] Response: Success (HTTP 200)
j= 1 , colcount= 50 , Rowid =  1139670832965508 , Guideline rowid=  , Section rowid=  , Newguideline=  , Newsectline=
Found guideline
[Smartsheet] 2021-08-02T05:26:37.433Z[   INFO] GET https://api.smartsheet.com/2.0/sheets/6458324490184580/rows/5643270460336004
[Smartsheet] 2021-08-02T05:26:38.268Z[   INFO] Response: Success (HTTP 200)
j= 2 , colcount= 50 , Rowid =  5643270460336004 , Guideline rowid= 1139670832965508 , Section rowid=  , Newguideline= 1 , Newsectline= 0
Found section
optionupdate for section = {
  sheetId: 6458324490184580,
  id: 5643270460336004,
  row: '[{"indent":1}]'
}
[Smartsheet] 2021-08-02T05:26:38.270Z[   INFO] PUT https://api.smartsheet.com/2.0/sheets/6458324490184580/rows5643270460336004
[Smartsheet] 2021-08-02T05:26:41.271Z[   INFO] GET https://api.smartsheet.com/2.0/sheets/6458324490184580/rows/3391470646650756
[Smartsheet] 2021-08-02T05:26:41.918Z[  ERROR] Request failed after 0 retries
[Smartsheet] 2021-08-02T05:26:41.919Z[  ERROR] PUT https://api.smartsheet.com/2.0/sheets/6458324490184580/rows5643270460336004
[Smartsheet] 2021-08-02T05:26:41.920Z[  ERROR] Response: Failure (HTTP 404)
        Error Code: 1006 - Not Found
        Ref ID: 8ek93cztzuuq
Unhandled rejection (<{"statusCode":404,"errorCode":1006,"me...>, no stack trace)

请注意,PUT 语句在 URL 中的单词行之后没有尾随 /,但我使用完全相同的构造来构建查询的选项。相反, GET 语句可以,并且一切正常。如果我尝试强制使用 'id: "/" + rowid' (我的行 ID 变量)建立选项,那么它(当然)将数字更改为字符串并且失败:-(

非常感谢任何提示/指导!

Bowow99

4

1 回答 1

0

嗨,api描述了以下参数:

var options = {  sheetId: 2068827774183300,  body: row  };

所以,你应该发送一些东西,比如用适当的值替换:

optionupdate= {
   sheetId: sheetno,
   body :  [{"id": "rowid", "parentId": "sectrow", "toBottom": "true"}])
};
于 2021-08-02T11:39:49.517 回答