0

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.

Isaaclele
  • 29
  • 6

1 Answers1

0

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.

Kim Brandl
  • 13,125
  • 2
  • 16
  • 21
  • Happy to help. Please mark this answer as 'accepted' if it gives you the info you need, as that'll make others more likely to benefit from the info in the future. BTW - you did a really good job asking this question - i.e., you clearly explained what you're trying to do AND also provided code showing how you're attempting to do it. This shows that you at least tried to figure it out on your own before posting here, and makes folks more likely to provide feedback here :) – Kim Brandl Jun 21 '21 at 14:45
  • Hey Kim, I really appreciate your pointers on my question, thanks for such a warm welcome to Stackoverflow. Your process did work and I was returned a string of the row_ids. However, I had to convert them to integers to use it in the move_rows function. To do this I used the following code: row_ids = list(map(int, row_ids.split(','))). When I run the code now, I receive no errors, yet, when I look at the sheet the rows should be sent to, nothing is added but empty rows. I know there may not be a direct answer to this, but it could be beneficial to future "questioners". – Isaaclele Jun 21 '21 at 15:20
  • Regarding my comment above, make sure the excel sheet you are importing has the same column headers as the sheet you are importing to. Once that is done everything works fine. – Isaaclele Jun 21 '21 at 16:52
  • Thanks for providing this additional information, so that others can benefit from it in the future. I've updated my answer to include this info, as folks sometimes don't read all the way through the comments. – Kim Brandl Jun 21 '21 at 18:28
  • Hey Kim, follow up question, when moving rows is there a way to specify to add the rows to the top of the destination sheet? – Isaaclele Jun 24 '21 at 19:23