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:
JavaScript
x
8
1
for A in AURL:
2
print(A)
3
driver.get(A)
4
imagelanding = driver.find_elements(By.CSS_SELECTOR, 'img[id="landingImage"]')
5
imglinks = [i.get_attribute('data-old-hires') for i in imagelanding]
6
print(imglinks)
7
sh.values_append('PhotoOnlyFeed!B:B',{'valueInputOption': 'USER_ENTERED'},{'values':[imglinks]})
8
update regarding pattern 1. Values are correct, but off by one row:
Advertisement
Answer
In your situation, how about the following patterns?
Pattern 1:
In this pattern, values_append
is used.
JavaScript
1
11
11
1
values = []
2
for A in AURL:
3
print(A)
4
driver.get(A)
5
imagelanding = driver.find_elements(By.CSS_SELECTOR, 'img[id="landingImage"]')
6
imglinks = [i.get_attribute('data-old-hires') for i in imagelanding]
7
print(imglinks)
8
values.append(imglinks)
9
10
sh.values_append('PhotoOnlyFeed!B:B',{'valueInputOption': 'USER_ENTERED'},{'values':values})
11
Pattern 2:
In this pattern, update
is used.
JavaScript
1
13
13
1
values = []
2
for A in AURL:
3
print(A)
4
driver.get(A)
5
imagelanding = driver.find_elements(By.CSS_SELECTOR, 'img[id="landingImage"]')
6
imglinks = [i.get_attribute('data-old-hires') for i in imagelanding]
7
print(imglinks)
8
values.append(imglinks)
9
10
worksheet = sh.worksheet("PhotoOnlyFeed")
11
last_row = len(worksheet.col_values(2))
12
worksheet.update('B' + str(last_row + 1), values, value_input_option='USER_ENTERED')
13
- 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 modifylen(worksheet.col_values(2))
.