我遇到了 smartsheet API 的问题。我可以很好地修改源智能表的内容,但我需要将某些内容从一张纸复制到一张新的空白纸中。主要问题在于行和列已分配了 ID,而目标工作表完全是空白的。有没有简单的方法可以根据需要创建具有尽可能多列的行,而无需指定column_ids,行parent_ids等?
我尝试将从旧工作表复制的行添加到新工作表。由于column_id不匹配,这不起作用。我尝试将复制行的每个column_id设置为新工作表中相应的column_ids,但是该行的parent_id不匹配
实现您所描述的场景将分为两步。
第1步:复印表
首先,使用复制工作表操作创建源工作表的空副本。
默认情况下,工作表的新副本将是一个空工作表,其“结构化”类似于源工作表(例如,相同的列数、列类型和名称等)——但它不会包含任何数据或格式源表中的、附件、讨论、表单、过滤器等。 如果您确实希望新工作表包含源工作表中的所有相同数据和格式、附件、讨论等,您可以在 API 请求中指定
include
参数来指示您想要的内容类型包括。 (有关此参数的有效值的详细信息,请参阅
API 文档。) 第 2 步:将源工作表中的行复制到新工作表
创建新工作表后,您可以使用“将行复制到另一个工作表”操作将指定行从源工作表复制到新工作表。
在请求中指定 include
参数,以指示您要复制到目标工作表的内容类型 - 即
all
、
discussions
、attachments
、children
。 (有关这些参数值的详细信息,请参阅 API 文档。)以下代码示例说明了我描述的两步过程。
'''
STEP 1: Create a copy of the source sheet.
Just copy sheet structure -- don't include any data, formatting, attachments, discussions, etc.
Name the new sheet My New Sheet and create it in the folder that has the specified folder_id.
'''
# specify the ID of the sheet to be copied
source_sheet_id = 2702602705784708
response = smartsheet_client.Sheets.copy_sheet(
source_sheet_id, # sheet_id
smartsheet.models.ContainerDestination({
'destination_type': 'folder', # folder, workspace, or home
'destination_id': 7825486215702404, # folder_id
'new_name': 'My New Sheet' # name of the new sheet
})
)
# specify the ID of the (newly created) sheet
destination_sheet_id = response.result.id
'''
STEP 2:
Copy specified rows from the source sheet to the destination sheet.
'''
source_sheet_row_ids = [409928880836484, 7165328321892228]
# copy rows from source sheet to (bottom of) destination sheet
# (include the attachments, children, and discussions)
response = smartsheet_client.Sheets.copy_rows(
source_sheet_id,
smartsheet.models.CopyOrMoveRowDirective({
'row_ids': source_sheet_row_ids,
'to': smartsheet.models.CopyOrMoveRowDestination({
'sheet_id': destination_sheet_id
})
}),
'children'
)
在此示例中,我的源表如下所示 - 请注意,第一行有 2 个“子”行,第二行包含讨论,最后一行包含附件:
运行上面的代码(指定源工作表中第一行和最后一行的 ID 作为要复制的行)后,(新创建的)目标工作表如下所示:
请注意,源工作表中的讨论和附件并未复制到目标工作表中——因为我的代码指定仅 children
(即指定行的子行)包含在复制到目标工作表的数据中。新表。