Why am I seeing an 'AttributeError' when using the Python Smartsheet SDK to create a new sheet in a folder?

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

Why am I seeing an 'AttributeError' when using the Python Smartsheet SDK to create a new sheet in a folder?

问题

我正在尝试使用Python SDK和API在Smartsheet中创建一个新工作表,成功地通过在终端中运行pip install smartsheet-python-sdk安装了SDK。然而,当我尝试通过Folder对象下的create_sheet_in_folder方法在文件夹中创建新工作表时,我的IDE中没有显示该方法,但我已经在我的设备上保存了folders.py。我已经验证了我的API密钥有效,可以向工作表中添加列,但出于某种原因,似乎我不能真正做太多关于创建新工作表的事情。

以下是参考的代码:

# Smartsheet API Challenge
import smartsheet

# 设置您的Smartsheet API访问令牌
access_token = "ACCESS_TOKEN"
smartsheet_client = smartsheet.Smartsheet(access_token=access_token)

folder_id = "FOLDER_ID"
# 创建一个新工作表
sheet_spec = smartsheet.Smartsheet.models.Sheet({
'name' : 'Sheet B',
'columns' : [{
'title' : 'Primary Column',
'primary' : True,
'type' : 'TEXT'
}, {
'title' : 'Col 1',
'type' : 'TEXT'
}
]
})

# 创建新工作表
response = smartsheet_client.models.Folder.create_sheet_in_folder(folder_id, sheet_spec)
new_sheet = response.result

以下是我收到的错误:

response = smartsheet_client.models.Folder.create_sheet_in_folder(folder_id, sheet_spec)

AttributeError: type object 'Folder' has no attribute 'create_sheet_in_folder'

希望这能帮助您解决问题。

英文:

I'm trying to use the Python SDK and API to create a new sheet in Smartsheet and was successful in installing the SDK via pip install smartsheet-python-sdk in my terminal. However, when I try to create a new sheet in a folder via the create_sheet_in_folder under the Folder object I'm not able to as that method is not showing up in my IDE, but I have folders.py saved on my device. I've verified my API key works by adding columns into a sheet but for some reason it looks like I can't really do much with creating a new sheet.

Here's my code for reference:

#Smartsheet API Challenge
import smartsheet

# Set your Smartsheet API access token
access_token = "ACCESS_TOKEN"
smartsheet_client = smartsheet.Smartsheet(access_token=access_token)

folder_id = "FOLDER_ID"
# Create a new sheet
sheet_spec = smartsheet.Smartsheet.models.Sheet({
'name' : 'Sheet B',
'columns' : [{
'title' : 'Primary Column',
'primary' : True,
'type' : 'TEXT'
}, {
'title' : 'Col 1',
'type' : 'TEXT'
}
]
})

#make the new sheet
response = smartsheet_client.models.Folder.create_sheet_in_folder(folder_id,sheet_spec)
new_sheet = response.result

Here's the error I get:

response = smartsheet_client.models.Folder.create_sheet_in_folder(folder_id,sheet_spec)

AttributeError: type object 'Folder' has no attribute 'create_sheet_in_folder'

答案1

得分: 0

代码中存在两个问题。

首先,调用create_sheet_in_folder方法的那行应该是:

response = smartsheet_client.Folders.create_sheet_in_folder(folder_id, sheet_spec)

其次,每个列的type属性值应该设置为TEXT_NUMBER(而不是TEXT):

'type': 'TEXT_NUMBER'

进行这些更改后,你的代码应该能够成功在指定的文件夹中创建新的工作表。

英文:

There are two issues with the code you posted.

First, the line that calls the create_sheet_in_folder method should be:

response = smartsheet_client.Folders.create_sheet_in_folder(folder_id,sheet_spec)

And second, the type attribute value for each column should be set to TEXT_NUMBER (not TEXT):

'type' : 'TEXT_NUMBER'

Make these changes and your code should successfully create the new sheet in the specified folder.

huangapple
  • 本文由 发表于 2023年6月2日 09:34:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386632.html
匿名

发表评论

匿名网友

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

确定