Been a frustrating day or so :-(
I would be very happy to be told I am going crazy (if you could point out the error of my ways).
I am trying to cycle through a smartsheet (only 900 rows) and automatically indent based on the contents of a column (called marker - in this case the 51st column). If marker is guideline then it is the top level parent, if it's section then it is a child of the guideline and if it is blank then it is a child of the section. Guidelines can have multiple sections and sections can have multiple (blank) children.
My code is still embryonic and far from elegant :-( I just wanted to get it working!!! I will return to tidy it up as soon as it is running. You can see I have tried a few different things.
First the code (and then the error I get is below that).
//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()
Now the error that I get...
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)
Notice in the error that the PUT statement does NOT have the trailing / after the word rows in the URL, and yet I use exactly the same construct to build the options up for the query. Conversely the GET statement does and everything works fine. If I try to force in in the options build up with 'id: "/" + rowid' (my variable for the row ID) then it (of course) changes the number to a string and that fails :-(
Any tips/guidance VERY much appreciated!
Bowow99
Hi the api descibes the parameters such as :
var options = { sheetId: 2068827774183300, body: row };
So, you should send something such as replace with appropriate values :
optionupdate= {
sheetId: sheetno,
body : [{"id": "rowid", "parentId": "sectrow", "toBottom": "true"}])
};