2

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

Ahira
  • 47
  • 4

1 Answers1

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:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thank you! Pattern 1 almost worked correctly, but it skipped the first row of column B. So data was off by one row. – Ahira Apr 19 '22 at 00:50
  • @Ahira Thank you for replying. I apologize for the inconvenience and my poor English skill. Unfortunately, I cannot imagine `Pattern 1 almost worked correctly, but it skipped the first row of column B. So data was off by one row.`. So, can I ask you about the detail of it? By this, I would like to modify it. – Tanaike Apr 19 '22 at 00:51
  • Pattern 2 worked correctly. I will update the questions with the result for pattern 1. – Ahira Apr 19 '22 at 00:54
  • @Ahira Thank you for replying. I understood that pattern 2 worked. I'm worried that `values_append` might not be able to be used in your Spreadsheet. So, I added the sample script using `update`. So, if you want to use pattern 1, in that case, please show your Spreadsheet as an image? By this, I would like to check it. – Tanaike Apr 19 '22 at 00:58
  • @Ahira Now, I saw your added image. In your situation, the values have already been existing in the columns "A" and "C". Under this condition, you want to put the values to the column "B". Is my understanding correct? If my understanding is correct, unfortunately, in that case, `values_append` cannot be used. Because `values_append` puts the values to the last row of the sheet. In that case, please use my pattern 2 using `update`. – Tanaike Apr 19 '22 at 01:12
  • @Ahira Thank you for replying and testing it. I'm glad your issue was resolved. Thank you, too. – Tanaike Apr 19 '22 at 01:24