接入教程
1. 注册Smartsheet账号并创建API访问令牌
2. 在请求头中配置Authorization: Bearer {您的令牌}
3. 使用GET /sheets获取工作表列表
4. 通过POST /sheets/{sheetId}/rows添加新数据行
5. 利用webhook功能设置实时数据更新通知
获取工作表列表
import requests
url = 'https://api.smartsheet.com/2.0/sheets'
headers = {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
sheets = response.json()
print(f'Found {len(sheets["data"])} sheets')
else:
print(f'Error: {response.status_code}')
print(response.text)
创建工作表
<?php
$url = 'https://api.smartsheet.com/2.0/sheets';
$apiKey = 'YOUR_API_KEY';
$data = [
'name' => 'New Project Sheet',
'columns' => [
['title' => 'Task', 'type' => 'TEXT_NUMBER'],
['title' => 'Status', 'type' => 'PICKLIST', 'options' => ['Not Started', 'In Progress', 'Completed']]
]
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json'
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode == 200) {
echo 'Sheet created successfully';
} else {
echo 'Error: ' . $httpCode;
echo $response;
}
?>
添加行到工作表
const fetch = require('node-fetch');
async function addRowsToSheet(sheetId) {
const url = `https://api.smartsheet.com/2.0/sheets/${sheetId}/rows`;
const rowsData = {
rows: [
{
cells: [
{ columnId: 1234567890, value: 'Design Phase' },
{ columnId: 2345678901, value: 'In Progress' }
]
}
]
};
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify(rowsData)
});
if (response.ok) {
const result = await response.json();
console.log(`Added ${result.result.length} rows successfully`);
} else {
console.error(`Error: ${response.status}`);
console.error(await response.text());
}
} catch (error) {
console.error('Request failed:', error);
}
}
// Usage
addRowsToSheet('1234567890123456');
常见问题
如何获取API密钥?
登录Smartsheet账户,进入个人设置 > API访问,生成新的访问令牌。请妥善保管您的API密钥,不要泄露给他人。
API请求频率有限制吗?
是的,Smartsheet API有速率限制。标准账户每分钟最多100个请求,企业账户可能更高。建议合理规划请求频率并实现适当的错误重试机制。
支持哪些数据格式?
API主要使用JSON格式进行数据交换。请求和响应都采用JSON格式。上传文件时可能需要使用multipart/form-data格式。
Aitishiku.com