激怒了 - 这是一个 smartsheet API 2.0 错误,还是(更有可能)我疯了?将 updateRow 与 Node.js 结合使用
Exasperated - Is it a smartsheet API 2.0 bug, or (more likely) am I going nuts? Using updateRow with Node.js
大约是令人沮丧的一天:-(
如果有人告诉我我快疯了,我会很高兴(如果你能指出我方法的错误)。
我正在尝试循环浏览智能表(仅 900 行)并根据列(称为标记 - 在本例中为第 51 列)的内容自动缩进。如果标记是指南那么它是顶级父级,如果它是部分那么它是指南的子级,如果它是空白那么它是该部分的子级。指南可以有多个部分,部分可以有多个(空白)子项。
我的代码还处于萌芽阶段,远谈不上优雅:-(我只是想让它工作!!!我会return尽快整理它运行。你可以看到我尝试了一些不同的东西。
首先是代码(然后我得到的错误在代码下方)。
//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 变量)构建的选项中强制加入,那么它(当然)将数字更改为字符串并且失败:-(
任何 tips/guidance 非常感谢!
Bowow99
您好api描述参数如:
var options = { sheetId: 2068827774183300, body: row };
因此,您应该发送一些内容,例如用适当的值替换:
optionupdate= {
sheetId: sheetno,
body : [{"id": "rowid", "parentId": "sectrow", "toBottom": "true"}])
};
大约是令人沮丧的一天:-(
如果有人告诉我我快疯了,我会很高兴(如果你能指出我方法的错误)。
我正在尝试循环浏览智能表(仅 900 行)并根据列(称为标记 - 在本例中为第 51 列)的内容自动缩进。如果标记是指南那么它是顶级父级,如果它是部分那么它是指南的子级,如果它是空白那么它是该部分的子级。指南可以有多个部分,部分可以有多个(空白)子项。
我的代码还处于萌芽阶段,远谈不上优雅:-(我只是想让它工作!!!我会return尽快整理它运行。你可以看到我尝试了一些不同的东西。
首先是代码(然后我得到的错误在代码下方)。
//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 变量)构建的选项中强制加入,那么它(当然)将数字更改为字符串并且失败:-(
任何 tips/guidance 非常感谢!
Bowow99
您好api描述参数如:
var options = { sheetId: 2068827774183300, body: row };
因此,您应该发送一些内容,例如用适当的值替换:
optionupdate= {
sheetId: sheetno,
body : [{"id": "rowid", "parentId": "sectrow", "toBottom": "true"}])
};