Home > database >  Append value of variable to specific colum in google sheets
Append value of variable to specific colum in google sheets

Time:04-19

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

CodePudding user response:

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:

  • Related