Выполните итерацию по строкам в smartsheet и сделайте отступ на основе значения в столбце, используя node.js

#node.js #smartsheet-api

Вопрос:

Мой Smartsheet и node.js мир расширяется :-), но теперь я не знаю, как к этому подойти. У меня есть плоский структурированный смарт-лист с примерно 1000 строками, и мне нужно сделать отступы в выбранных строках, используя node.js. Система (для этого примера) разбита на три уровня: глава, Раздел и Параграф. У меня есть столбец (называемый маркером), который идентифицирует слой по имени (см. Пример ниже).

Что мне нужно сделать, так это сделать отступы между разделами под главами и абзацами под разделами. Обратите внимание, что глава может содержать несколько разделов. (Также обратите внимание, что пример именно такой. Я не могу предоставить реальные данные, извините.)

[обновление 1 августа 2021 года]

Я ПОЧТИ там (мучительно близко)…

Я думаю, что одна из последних вещей, которые мне нужно будет сделать, прежде чем оптимизировать и документировать, — это заставить бит отступа работать. Мой код приведен ниже…

 //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)
 

Не уверен, что я делаю неправильно, но очевидно, что это как-то связано с кодом раздела.

Ответ №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!  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()