Isaaclele
Isaaclele

Reputation: 39

How to iterate through the row property of the sheet object and create a comma delimited list of row id's?

I am currently trying to import an excel sheet into Smartsheet, then take the rows of the imported sheet, and move them to the bottom of an existing sheet. To do this I am using the Sheets.move_row function. Below is a snippet of that code.

response = smart.Sheets.move_rows(
result.data.id,smart.models.CopyOrMoveRowDirective({
    'row_ids': [**Help**],
    'to': smart.models.CopyOrMoveRowDestination({'sheet_id': 1174866712913796})}))

To get information on the imported sheet I use the get_sheet command. My plan would be to then iterate through the sheet.row property and find where "id" is listed and then pull the number next to id into a comma delimited list.

Below is a snippet of me attempting to iterate through the row property, but I am unsure of how to pull out the row ids, and then put them into a comma delimited list.

 sheet_info = smart.Sheets.get_sheet(result.data.id,_dir)
print(sheet_info)

for id in sheet_info.rows:
    x = id
print (x)  #this just prints the cells category

Any help would be appreciated, thanks. For further clarification on what I am trying to do please reference my previously posted question.

Upvotes: 0

Views: 390

Answers (1)

Kim Brandl
Kim Brandl

Reputation: 13500

The following code snippet does what you've described.

sheetId = 3932034054809476

# get the sheet
sheet = smart.Sheets.get_sheet(sheetId) 

# iterate through the rows array and build comma-delimited list of row ids
row_ids = ''
for row in sheet.rows:
    row_ids += str(row.id) + ', '

# remove the final (excess) comma and space from the end of the row_ids string
row_ids = row_ids[:len(row_ids)-2]

print(row_ids)

UPDATE:

As @Isaaclele mentions in the comments below, the Copy or Move Row(s) operation requires that the rowIds parameter be specified as a number[]. The string value of the row_ids property in the code snippet above can be converted to this format as shown here:

row_ids = list(map(int, row_ids.split(',')))

Also note (as mentioned in the comments below) that the Copy or Move Row(s) operation requires the column names in the source sheet and the destination sheet to match exactly.

Upvotes: 0

Related Questions