遍历工作表并追加列

时间:2018-11-15 04:27:46

标签: smartsheet-api smartsheet-api-2.0

我在Smartsheet工作区中有100多个工作表。我想使用python api遍历每个工作表并将行ID,工作表ID和主要列附加到数组或熊猫数据框中。

import requests
import pandas as pd
import io
import warnings
warnings.filterwarnings(action='once')
import smartsheet
import os.path
from time import gmtime, strftime
import pandas as pd
import numpy as np
import json
import requests
import hashlib

fullList = pd.DataFrame()

for sheet in sheetArray:
    r = requests.get(baseURL + "/" + str(sheet), headers=headers)
    rows = json.loads(r.text)
    rows = rows['rows']
    rowsDF = pd.DataFrame.from_dict(rows)
    dropCols = ["cells","createdAt", "expanded", "modifiedAt","siblingId"]
    rowsDF = rowsDF.drop(dropCols, axis=1)
    fullList.append(rowsDF)

1 个答案:

答案 0 :(得分:3)

我不确定熊猫,但是我可以帮助您将信息获取到python数组中。

使用Smartsheet Python SDK,首先要install the SDK,然后是import smartsheet

接下来,像这样用access token初始化Smartsheet对象

ss_client = smartsheet.Smartsheet(SMARTSHEET_ACCESS_TOKEN)

获取您的工作空间

workplace = ss_client.Workspaces.get_workspace(workplace_id)

从工作区抓纸

wp_sheets = workplace.sheets

初始化要创建的数组

info_array = []

从“工作区”对象中套叠图纸。这些工作表对象只有几个字段可以标识工作表,因此您需要使用sheet.id从Smartsheet API中获取整个工作表。

# loop through sheets 
for sheet in wp_sheets:
    # get sheet
    full_sheet = ss_client.Sheets.get_sheet(sheet.id)

获取工作表的主列

# get the primary column
primary_column_id = get_primary_column_id(full_sheet.columns)

get_primary_column_id()函数如下所示。列对象具有primary的布尔字段。找到primary设置为true的列。

def get_primary_column_id(columns):
    for column in columns:
        if (column.primary):
            return column.id

获取行ID,并将所有信息附加到info_array

# get row ids
for row in full_sheet.rows:
    info_array.append({'sheet_id': sheet.id, 
    'row_id': row.id, 
    'primary_column_id': primary_column_id})  

这里是Gist

相关问题