【问题标题】:updating cells in gspread beyond 26更新 gspread 中超过 26 个的单元格
【发布时间】:2018-11-20 10:39:08
【问题描述】:

我正在尝试为我的工作自动化 google 表格上的排班系统(我在书店工作,我正在学习编码作为一种爱好)。我创建了两个列表,一个带有时间段,另一个带有我想在电子表格上打印的天数。

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

days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
daysIter = iter(days)
timeslots = ["8:00AM","8:30AM","9:00AM","9:30AM","10:00AM","10:30AM","11:00AM","11:30AM",
             "12:00PM","12:30PM","1:00PM","1:30PM","2:00PM","2:30PM","3:00PM","3:30PM","4:00PM","4:30PM",
             "5:00PM","5:30PM","6:00PM","6:30PM","7:00PM","7:30PM","8:00PM","8:30PM","9:00PM","9:30PM"]


timesIter = iter(timeslots)

daysonsheet = sheet.range("A2:A7")
timesonsheet = sheet.range("C1:AD1")

    


sheet.update_acell('A1',days[0])
for cell in daysonsheet:
    cell.value = next(daysIter)
    sheet.update_cells(daysonsheet)

sheet.update_acell('B1',timeslots[0])
for cell1 in timeslots:
    cell1.value = next(timesIter)
    sheet.update_cells(timesonsheet)
    
    
    
    

我可以很好地更新 daysonsheet,但 timesonsheet 会抛出:

  File "/Users/*******/Rostering2", line 52, in <module>
    cell1.value = next(timesIter)

AttributeError: 'str' object has no attribute 'value'

据我所知,它们都是相同的,但是 for 循环中有效的“单元格”不是作为对象创建的,而“cell1”是出于某种原因..

如果有人能对此有所了解,那就太好了。

【问题讨论】:

    标签: python gspread


    【解决方案1】:

    代码中的timeslots 是一个字符串列表,它没有.value 属性。您应该遍历Cell 对象列表,即timeonsheet。此外,请考虑将 sheet.update_cells 调用移出循环以节省 API 调用。

    for cell1 in timesonsheet:
        cell1.value = next(timesIter)
    sheet.update_cells(timesonsheet)
    

    【讨论】:

    • 也为错误的标题道歉,这是上一期忘记换标题了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多