Reputation: 69
My Smartsheet and node.js world is expanding :-), but now something I don't know how to approach. I have a flat structured smartsheet with around 1000 rows and I need to indent selected rows using node.js. The system (for this example) is broken into three layers; Chapter, Section, and Paragraph. I have a column (called Marker) which identifies the layer by name (see example below).
What I need to do is to indent the sections below the chapters, and the paragraphs below the sections. Note that a chapter can contain multiple sections. (Also note that the example is just that. I can't give real data, sorry.)
[update 1 Aug 2021]
I am ALMOST there (tantalisingly close)...
I think that one of the last things I will need to do, before optimising and documenting, is to get the indent bit working. My code is below...
//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()
So when I run it I get...
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)
Not sure what I am doing wrong, but clearly it's something to do with the section code.
Upvotes: 1
Views: 571
Reputation: 69
What I needed to do is to indent the sections below the chapters, and the paragraphs below the sections. Note that a chapter can contain multiple sections. (Also note that the example is just that. I can't give real data, sorry.)
As I had problems getting it working I thought it would be useful for someone else once I succeeded :-). Yet to be optimised and documented.
//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()
Upvotes: 1