Smartsheet API Python 更新列 4004 错误

huangapple go评论86阅读模式
英文:

Smartsheet API Python update columns 4004 error

问题

我最近一直在使用Python为客户开发一些自动化功能。客户有新的活动,他们希望将这些新活动动态添加到一个用于跟踪活动的工作表的下拉列中。我已经开发了一个脚本来完成这个任务,它可以正常工作。脚本从一个工作表中提取数据(客户通过表单发送新的活动名称),然后更新另一个工作表中的下拉列表。

我的问题是,我偶尔会遇到500 4004错误,但我不知道原因。我可以在Jupyter Notebook中一整天运行这个脚本而不会出现问题。然而,当我将脚本设置为cron定时运行时,就会出现错误。我不确定这会如何改变代码执行和授权令牌的使用。感谢任何帮助或见解。

脚本:

  1. import smartsheet
  2. import os
  3. # 将访问令牌设置为操作系统环境变量
  4. os.environ['SMARTSHEET_ACCESS_TOKEN'] = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
  5. # 初始化Smartsheet模块
  6. smart = smartsheet.Smartsheet()
  7. # 获取将接收新下拉选项列表的主工作表
  8. # 定义工作表ID
  9. ############################## 插入下面的工作表ID #################################
  10. main_sheet = smart.Sheets.get_sheet(XXXXXXXXXXXXXXXX)
  11. sheet_id = main_sheet.id
  12. # 定义主工作表的下拉列号
  13. # 插入从零开始的列号 #################################
  14. pick_col = 6
  15. # 定义下拉列的列ID
  16. col = main_sheet.columns[pick_col].id
  17. ##
  18. ## 从单独的选项工作表中提取选项
  19. ##
  20. # 获取包含新下拉选项列表的列的工作表,用于FOR循环范围
  21. opt_sheet = smart.Sheets.get_sheet(XXXXXXXXXXXXXXXX)
  22. opt_ID = opt_sheet.id
  23. usda_opt = smart.Sheets.get_sheet(opt_ID, column_ids=opt_sheet.columns[0].id)
  24. # 定义用于FOR循环范围的行数变量
  25. num_rows1 = usda_opt.total_row_count
  26. # 获取包含下拉选项列表的列中的所有值。
  27. # 检查值是否为None类型,如果是,则中断for循环。这确保了所有最近添加的值都包括在其中,直到随后的空单元格。
  28. # 注意:根据您的工作表,更改单元格下标数字以正确的从零开始的单元格编号
  29. new_options = []
  30. for i in range(num_rows1):
  31. if opt_sheet.rows[i].cells[0].value == None:
  32. pass
  33. else:
  34. new_options.append(opt_sheet.rows[i].cells[0].value)
  35. # 使用新列选项和标题构建列对象。
  36. # 索引是从左到右计算的下拉列的位置(从零开始)
  37. col_list = smart.models.Column({
  38. 'title': main_sheet.columns[pick_col].title,
  39. 'type': 'PICKLIST',
  40. 'options': new_options,
  41. 'index': pick_col
  42. })
  43. # 将列对象发送以更新列下拉选项
  44. updated_col = smart.Sheets.update_column(sheet_id, col, col_list)

Cron错误邮件

该代码使用update_column而不是update_rows,所以我不确定它为什么会偶尔抛出错误。我已经调整了cron的频率,但似乎没有明显的影响。

英文:

I've recently been developing some automation for a client using Python. The client has new events they put on and wants those new events dynamically added to a drop down column in a sheet for tracking events. I've developed a script to do just that, and it works. The script pulls data from one sheet (client sends new event names via a form) and then updates the drop down in another sheet.

My issue is that I get intermittent 500 4004 errors and I'm not sure why. I can run this all day through Jupyter Notebook with no issue. However, when I set the script up to run as a cron I get the error. Not sure what that would change in the code execution and auth token usage. Any help or insight is appreciated.

Script:

  1. import smartsheet
  2. import os
  3. # Set access token to OS environment
  4. os.environ['SMARTSHEET_ACCESS_TOKEN'] = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
  5. # Initiate Smartsheet module
  6. smart = smartsheet.Smartsheet()
  7. # Get the main sheet which will receive the new dropdown option list
  8. # Define the sheet ID
  9. ############################## INSERT SHEET ID BELOW #################################
  10. main_sheet = smart.Sheets.get_sheet(XXXXXXXXXXXXXXXX)
  11. sheet_id = main_sheet.id
  12. # Define the dropdown column number for main sheet
  13. # INSERT ZERO-BASED COLUMN NUMBERS BELOW #################################
  14. pick_col = 6
  15. # Define the column ID of the dropdown column
  16. col = main_sheet.columns[pick_col].id
  17. ##
  18. ## Pull options from separate options sheet
  19. ##
  20. # Get the sheet containing the columns with the lists of new dropdown options for FOR loop range
  21. opt_sheet = smart.Sheets.get_sheet(XXXXXXXXXXXXXXXX)
  22. opt_ID = opt_sheet.id
  23. usda_opt = smart.Sheets.get_sheet(opt_ID, column_ids = opt_sheet.columns[0].id)
  24. # Define the number of rows variable used for the range in the FOR loop
  25. num_rows1 = usda_opt.total_row_count
  26. # Grab all the values in the column which holds the list of dropdown options.
  27. # Check if the value is None type and break the for-loop if it is. This ensures
  28. # all recently-added values are included up to the subsequent empty cell.
  29. # NOTE: CHANGE THE CELL SUBSCRIPT NUMBER TO THE CORRECT ZERO-BASED CELL NUMBER FOR YOUR SHEET
  30. new_options = []
  31. for i in range(num_rows1):
  32. if opt_sheet.rows[i].cells[0].value == None:
  33. pass
  34. else:
  35. new_options.append(opt_sheet.rows[i].cells[0].value)
  36. # Build the column object with the new column options and title.
  37. # The index is the location of the dropdown column counting from left to right (zero-based)
  38. col_list = smart.models.Column({
  39. 'title':main_sheet.columns[pick_col].title,
  40. 'type':'PICKLIST',
  41. 'options': new_options,
  42. 'index': pick_col
  43. })
  44. # Send the column object off to update the column dropdown options
  45. updated_col = smart.Sheets.update_column(sheet_id, col, col_list)

Cron error mail

The code uses update_column instead of update_rows, so I'm not sure why it will intermittently throw the error it does. I've adjusted the frequency of the cron, which doesn't seem to have any noticeable effect.

答案1

得分: 0

Ok,我认为我解决了这个问题。我有两个不同的Python脚本在使用相同的API令牌。我将它们合并成一个单独的脚本。到目前为止一切都很好!

英文:

Ok I think I solved the issue. I had two separate Python scripts that were utilizing the same API token. I was running them concurrently as two separate cron jobs. I combined them into a single script. So far so good!

huangapple
  • 本文由 发表于 2023年7月11日 04:17:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657063.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定