I need to pull both a username and an extension from a Google sheet and then email it to the users once a week. I've written an extremely inelegant solution that I will attach below. I'm certain it can be reduced substantially with a well-written loop and some logic. I'd appreciate any nudge in the right direction. I started working on this two days ago with zero programming knowledge so please excuse any obvious blunders!
I'd need to put in each row_number()+'obfuscated.com' in every subject line and then insert row_number() into every body.
EDIT: For clarification, I simply manually wrote in each row variable by hand. The number of users changes weekly, but I wrote it up to forty as it will never go above that.
from oauth2client.service_account import ServiceAccountCredentials
json_key = json.load(open('obfuscated'))
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('obfuscated.json',
gc = gspread.authorize(credentials)
sh = gc.open_by_key('obfuscated')
worksheet = sh.get_worksheet(0)
row_1 = worksheet.row_values(2)
row_2 = worksheet.row_values(3)
Here you go, it took forever for me to finally get a setup using your data and setting up an oauth to use with gspread but after that it's super simple.
currentRow = 2 while 1: thisRow = worksheet.row_values(currentRow) if (thisRow == ""): break username = thisRow + "@obfuscated.com" personalExtension = thisRow #email sending code goes here print(username, personalExtension) currentRow += 1
You just get item at index
 for username and item at index
 for personal extension. It loops until it hits a row with no personal extension. This will work with any number of emails.