将变量的值附加到Google表中的特定Colum

发布于 2025-01-22 03:36:49 字数 460 浏览 1 评论 0 原文

我想在电子表格中写入第二列的变量的值。以下代码适用于第一次迭代。随后的迭代在第一列中添加为新行:

for A in AURL:
    print(A)  
    driver.get(A)
    imagelanding = driver.find_elements(By.CSS_SELECTOR, 'img[id="landingImage"]')
    imglinks = [i.get_attribute('data-old-hires') for i in imagelanding]
    print(imglinks)
    sh.values_append('PhotoOnlyFeed!B:B',{'valueInputOption': 'USER_ENTERED'},{'values':[imglinks]})

有关模式1的更新。值正确,但按一行关闭:

I'd like to write the value of a variable to the 2nd column in a spreadsheet. The below code works for the first iteration. Subsequent iterations are added as new rows in the first column:

for A in AURL:
    print(A)  
    driver.get(A)
    imagelanding = driver.find_elements(By.CSS_SELECTOR, 'img[id="landingImage"]')
    imglinks = [i.get_attribute('data-old-hires') for i in imagelanding]
    print(imglinks)
    sh.values_append('PhotoOnlyFeed!B:B',{'valueInputOption': 'USER_ENTERED'},{'values':[imglinks]})

update regarding pattern 1. Values are correct, but off by one row:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦回梦里 2025-01-29 03:36:49

在您的情况下,以下模式如何?

模式1:

在此模式中,使用 values_append

values = []
for A in AURL:
    print(A)  
    driver.get(A)
    imagelanding = driver.find_elements(By.CSS_SELECTOR, 'img[id="landingImage"]')
    imglinks = [i.get_attribute('data-old-hires') for i in imagelanding]
    print(imglinks)
    values.append(imglinks)

sh.values_append('PhotoOnlyFeed!B:B',{'valueInputOption': 'USER_ENTERED'},{'values':values})

模式2:

在此模式中,使用更新

values = []
for A in AURL:
    print(A)  
    driver.get(A)
    imagelanding = driver.find_elements(By.CSS_SELECTOR, 'img[id="landingImage"]')
    imglinks = [i.get_attribute('data-old-hires') for i in imagelanding]
    print(imglinks)
    values.append(imglinks)

worksheet = sh.worksheet("PhotoOnlyFeed")
last_row = len(worksheet.col_values(2))
worksheet.update('B' + str(last_row + 1), values, value_input_option='USER_ENTERED')
  • 在这种情况下,列“ B”的最后一行由 len(Worksheet.col_values(2))检索。如果要检索另一列的最后一行,请修改 len(worksheet.col_values(2))

参考:

In your situation, how about the following patterns?

Pattern 1:

In this pattern, values_append is used.

values = []
for A in AURL:
    print(A)  
    driver.get(A)
    imagelanding = driver.find_elements(By.CSS_SELECTOR, 'img[id="landingImage"]')
    imglinks = [i.get_attribute('data-old-hires') for i in imagelanding]
    print(imglinks)
    values.append(imglinks)

sh.values_append('PhotoOnlyFeed!B:B',{'valueInputOption': 'USER_ENTERED'},{'values':values})

Pattern 2:

In this pattern, update is used.

values = []
for A in AURL:
    print(A)  
    driver.get(A)
    imagelanding = driver.find_elements(By.CSS_SELECTOR, 'img[id="landingImage"]')
    imglinks = [i.get_attribute('data-old-hires') for i in imagelanding]
    print(imglinks)
    values.append(imglinks)

worksheet = sh.worksheet("PhotoOnlyFeed")
last_row = len(worksheet.col_values(2))
worksheet.update('B' + str(last_row + 1), values, value_input_option='USER_ENTERED')
  • In this case, the last row of the column "B" is retrieved by len(worksheet.col_values(2)). If you want to retrieve the last row of other column, please modify len(worksheet.col_values(2)).

References:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文