You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Smartsheet API从现有Sheet的JSON创建新Sheet报错求助

解决Smartsheet API创建新表时的"Field ... was of unexpected type"错误

你遇到的1008错误核心原因是:从现有Sheet导出的JSON包含大量Smartsheet自动生成的只读系统字段,而create_sheet_in_folder() API只接受用于创建新表的可写字段,这些多余字段的类型或存在性不符合API要求,所以会触发解析错误。只删除createdAt远远不够,需要批量清理所有这类字段。

需要移除的核心字段清单

Sheet根节点字段:

  • id
  • createdAt
  • modifiedAt
  • createdBy
  • modifiedBy
  • permalink
  • accessLevel
  • effectiveAttachmentOptions
  • sheetVersion

列(Columns)子节点字段:

  • id
  • createdAt
  • modifiedAt
  • createdBy
  • modifiedBy
  • index
  • systemColumnType

行(Rows)子节点字段(如需复制行内容则清理):

  • id
  • createdAt
  • modifiedAt
  • createdBy
  • modifiedBy
  • rowNumber
  • permalink

具体代码实现

以下是清理Sheet JSON并创建新表的完整示例:

import smartsheet

# 初始化客户端
ss_client = smartsheet.Smartsheet('YOUR_API_TOKEN')

# 获取源Sheet并转JSON
source_sheet = ss_client.Sheets.get_sheet(SOURCE_SHEET_ID)
sheet_json = source_sheet.to_json()

# 清理Sheet根节点只读字段
remove_sheet_fields = ['id', 'createdAt', 'modifiedAt', 'createdBy', 'modifiedBy', 'permalink', 'accessLevel', 'effectiveAttachmentOptions', 'sheetVersion']
for field in remove_sheet_fields:
    sheet_json.pop(field, None)

# 清理列字段
remove_column_fields = ['id', 'createdAt', 'modifiedAt', 'createdBy', 'modifiedBy', 'index', 'systemColumnType']
for column in sheet_json['columns']:
    for field in remove_column_fields:
        column.pop(field, None)

# 清理行字段(可选,需复制行内容时启用)
if 'rows' in sheet_json:
    remove_row_fields = ['id', 'createdAt', 'modifiedAt', 'createdBy', 'modifiedBy', 'rowNumber', 'permalink']
    for row in sheet_json['rows']:
        for field in remove_row_fields:
            row.pop(field, None)

# 批量替换场景示例:修改新表名称+替换单元格内容
sheet_json['name'] = f"{sheet_json['name']}_COPY"
search_text = "旧文本"
replace_text = "新文本"
for row in sheet_json['rows']:
    for cell in row['cells']:
        if 'value' in cell and isinstance(cell['value'], str):
            cell['value'] = cell['value'].replace(search_text, replace_text)
        if 'displayValue' in cell and isinstance(cell['displayValue'], str):
            cell['displayValue'] = cell['displayValue'].replace(search_text, replace_text)

# 创建新表
try:
    new_sheet = ss_client.Sheets.create_sheet_in_folder(FOLDER_ID, smartsheet.models.Sheet(sheet_json))
    print(f"新表创建成功,ID: {new_sheet.id}")
except smartsheet.exceptions.ApiError as e:
    print(f"创建失败: {e}")

批量替换场景说明

上述代码中已经包含了单元格内容的批量替换逻辑,无需提前知晓列名或ID,直接遍历所有单元格即可完成正则替换(将示例中的replace改为正则替换即可,比如用re.sub())。

内容的提问来源于stack exchange,提问作者Anachronous

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 01:44:55