如何使用pyghseets获取过滤的工作表单元格值 [英] How to get filtered sheet cells values with pyghseets

查看:93
本文介绍了如何使用pyghseets获取过滤的工作表单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何仅从电子表格中获取过滤后的值?如果用户过滤了一些值,则不需要它们.

How can I get only filtered values from spreadsheet? If user have filtered some values, I don't need them.

推荐答案

  • 您要从已过滤的工作表中检索值.
  • 过滤器是基本过滤器.
  • 例如,当将值放在单元格"A1:J25"中并且过滤器显示"A4:J4"和"A11:J11"的值时,您要检索"A4:J4"和"A11:J11".
  • 您要使用pygsheets实现此目的.
  • 您已经能够使用带有pygsheet的Sheets API来获取和放置Google Spreadsheet的值.
  • 如果我的理解是正确的,那么这个答案如何?请认为这只是几个可能的答案之一.

    If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

    在这个答案中,我针对这种情况使用了查询语言.首先,使用pygsheets检索访问令牌和工作表ID,然后使用查询语言"检索过滤后的工作表中的值.为此,我使用了请求".

    In this answer, I used Query Language for this situation. At first, the access token and sheet ID are retrieved using pygsheets, and then, the values from the filtered sheet are retrieved by the Query Language. For this, I used "requests".

    在使用此脚本之前,请设置变量.

    Before you use this script, please set the variables.

    import csv
    from io import StringIO
    import pygsheets
    import requests
    
    service_account_file = '###'  # Please set the JSON filename including the credentials of service account.
    spreadsheet_id = '###'  # Please set the Spreadsheet ID.
    sheet_name = 'Sheet1'  # Please set the sheet name.
    
    gc = pygsheets.authorize(service_file=service_account_file)
    sh = gc.open_by_key(spreadsheet_id)
    wks = sh.worksheet_by_title(sheet_name)
    sheet_id = wks.jsonSheet['properties']['sheetId']
    url = 'https://docs.google.com/spreadsheets/d/' + spreadsheet_id + '/gviz/tq?tqx=out:csv&gid=' + str(sheet_id)
    res = requests.get(url, headers={'Authorization': 'Bearer ' + gc.oauth.token})
    strValues = res.text
    f = StringIO(strValues)
    reader = csv.reader(f, delimiter=',')
    ar = [row for row in reader]
    ar.pop(0)
    print(ar)
    

    • 在此示例脚本中,我使用了服务帐户.如果您使用的是OAuth2,请修改授权脚本.
    • 将此脚本用于示例电子表格时,可以检索"A4:J4"和"A11:J11"单元格的值.
    • 这篇关于如何使用pyghseets获取过滤的工作表单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆