首页 > 解决方案 > 如何使用 ezsheets API for Google Sheets 冻结一行?

问题描述

我在 Python 中有一个漂亮的脚本,它接受 Spotify 专辑 URI 的输入,然后将它和一些信息添加到跟踪我听过的专辑的 Google 表格的底部。

当我运行脚本时,它似乎会擦除工作表顶部的冻结行。有没有办法在脚本结束时冻结工作表的第一行?

我尝试sheet.frozenRowCount(1)在最后使用冻结第一行,但出现错误。

到目前为止,这是我的代码:

#! /usr/bin/env python3

# import os
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import ezsheets

# set varialbes
cid = '{{ my cid }}'
secret = '{{ my secret key }}'
ss = ezsheets.Spreadsheet('{{ my spreadsheet }}')
sheet = ss[0]
num_rows = sheet.rowCount
spotify = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials(
    client_id=cid, client_secret=secret))

# get album and set variables
album_input_raw = input("What's the album ID? ")
genre = input("What's the genre? ").title()
country = input("What country is the artist from? ").title()
listen_again_raw = input("Will you listen again? ").upper()


if listen_again_raw == "YES" or listen_again_raw == "Y":
    listen_again_answer = "YES"
    love_it_raw = input("Is it a new favorite? ").upper()
    if love_it_raw == "YES" or listen_again_raw == "Y":
        love_it_answer = "YES"
    else:
        love_it_answer = ""
else:
    listen_again_answer = ""

album_input = album_input_raw[-22:]
album_id = f"spotify:album:{album_input}"
album_info = spotify.album(album_id)
album_name = album_info['name']
artist = album_info['artists'][0]['name']
year_raw = album_info['release_date']
year = year_raw[0:4]

# update the spreadsheet
sheet.updateRow(num_rows + 1, [album_name, artist, 'YES',
                               year, genre, '', country, listen_again_answer, love_it_answer])

sheet.frozenRowCount(1)

我收到此错误:

Traceback (most recent call last):
  File "/Users/scottsmith/Code/Projects/AddAlbumApp/add_album.command", line 45, in <module>
    sheet.frozenRowCount(1)
TypeError: 'int' object is not callable

标签: pythonspotipy

解决方案


据我所知,ezsheets 模块没有很好的文档。但是查看源代码我们可以看到有以下内容:

@frozenRowCount.setter
    def frozenRowCount(self, value):

因此,在您的代码中,如果您希望第一行保持冻结状态,您可以尝试这样的操作:

sheet.frozenRowCount(1)

推荐阅读