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

Python脚本选择下拉菜单后无法更新Google Sheets单元格

Google Sheets 脚本无法触发单元格更新问题

我有两个Google Sheets表格,分别为contacts和drop:

  • contacts表格的A列存储着链接列表
  • drop表格的A1单元格设置了下拉菜单,用于选择名称

我编写的Python脚本预期实现以下逻辑:监听drop表格A1单元格的内容变化,当用户选择新名称时,从contacts表格的A列随机选取一个链接,更新到drop表格的B1单元格。

脚本代码

import gspread
from oauth2client.service_account import ServiceAccountCredentials
import random
import time

# Google Sheets credentials and authorization
scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name("/home/teto/shaped-kite-414402-072de6ef4591.json", scope)
client = gspread.authorize(creds)

# Open both spreadsheets
contacts_sheet = client.open('contacts').sheet1
drop_sheet = client.open('drop').sheet1

# Get all the links from contacts spreadsheet
links = contacts_sheet.col_values(1)[1:]  # Exclude header

# Function to update the drop spreadsheet
def update_drop_sheet(name):
    random_link = random.choice(links)
    drop_sheet.update('B1', random_link)
    print(f"Random link for '{name}' added to B1")

# Main function to listen for changes in A1 cell and update B1
def main():
    previous_name = None
    while True:
        cell = drop_sheet.acell('A1', value_render_option='UNFORMATTED_VALUE')
        name = cell.value
        if name != previous_name:
            if name:
                update_drop_sheet(name)
            previous_name = name
        time.sleep(1)

if __name__ == '__main__':
    main()

我已经确认脚本使用的凭证有效,且drop表格的下拉菜单配置无误,但选择新名称后,B1单元格并未更新。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 07:12:40