简体   繁体   中英

trying to extract rowIds from smartsheet using node.js

I am trying to iterate through and existing smartsheet to get the rowIds (which I need for setting parent/child relationships).

I am struggling :-(

Obviously I don't get the required syntax on this one. Here is what I tried:

var options = {
 Id: *******************
};
var row_ids = '';

// get the sheet
smartsheet.sheets.getSheet(options) 
.then(function(sheetinfo) {
// iterate through the rows array and build comma-delimited list of row ids
for (rowIds in sheetinfo) {
    row_ids += row_ids.concat(', ');
}
});

console.log(row_ids);

Any help would be most appreciated.

Bowow99

In the Get Sheet response, sheetInfo contains a collection of rows and each row object in that collection contains the property id . So to successfully implement the scenario you've described, you need to loop through the sheetInfo.rows array, and for each row in that array, append the value of its id property (followed by a comma) to create the comma-delimited string that you're attempting to build. At the end of this loop, there'll be an extra comma at the end (following the last row id you added to the string) -- so you'll need to remove it using the substring function.

The following code does what I've described above. Please note, I'm not a JavaScript expert -- so there may be a more efficient way to do this...but this code gets you the end result you're after: a comma-delimited string of the row Ids within the specified sheet.

var options = {
    id: 8337220210845572 // Id of Sheet
};

var row_ids = '';

// Get sheet
smartsheet.sheets.getSheet(options)
    .then(function(sheetInfo) {
        // iterate through rows in the sheet and build comma-delimited string of row ids
        for (var i=0; i < sheetInfo.rows.length; i++) {
            row_ids += sheetInfo.rows[i].id + ','; 
        }
        // remove the final (excess) comma from the end of the row_ids string
        row_ids = row_ids.substring(0, row_ids.length-1);
        console.log(row_ids);
    })
    .catch(function(error) {
        console.log(error);
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM