写一个函数

时间:2018-01-11 04:15:19

标签: excel function user-defined-functions

所以我编写了一个工作正常的程序。它从基于股票代码的网站提取股票数据,然后根据我想要的行和列将其放入excel电子表格中。我希望能够用多种股票做到这一点,但我不知道如何编写函数。所以,我一直在做的是一遍又一遍地复制和粘贴代码。我的代码中唯一更改的参数是自动收报机和标记为r,x和y的行号。有人可以帮我写一个函数给我的程序吗?

fd = gm.FinancialsDownloader()
fd_frames = fd.download('AAPL')
wb = UpdateWorkbook(r'C:\Users\vince\Project\Stock Python Project\Spreadsheet.xlsx', worksheet=1)
i_s = fd_frames['income_statement']
i_s.set_index('title', inplace=True)
i_s = i_s.drop('parent_index', axis=1)
i_s = i_s.loc[['Revenue','Operating expenses']] #Add all the names you want from income statement
i_s = i_s/(10**9)

b_s = fd_frames['balance_sheet']
b_s.set_index('title', inplace=True)
b_s = b_s.drop('parent_index', axis=1)
b_s = b_s.loc[['Assets','Current assets']]
b_s = b_s/(10**9)

c_f = fd_frames['cash_flow']
c_f.set_index('title', inplace=True)
c_f = c_f.drop('parent_index', axis=1)
c_f = c_f.loc[['Free Cash Flow','Operating cash flow']]
c_f = c_f/(10**9)

r = 6  # starts at whatever row
c = 13 # column 'M'
for l in i_s.values.tolist():
    for item in l:
        wb.ws.cell(row=r, column=c).value = item
        c += 1 # Column 'N'
    c = 13
    r += 1

x = 21   
for l in b_s.values.tolist():
    for item in l:
        wb.ws.cell(row=x, column=c).value = item
        c += 1 # Column 'N'
    c = 13
    x += 1

y = 41
for l in c_f.values.tolist():
    for item in l:
        wb.ws.cell(row=y, column=c).value = item
        c += 1 # Column 'N'
    c = 13
    y += 1
wb.save()

1 个答案:

答案 0 :(得分:0)

您可以在纯JavaScript中创建一个函数,例如

AssignerWithPeriodicWatermarks<Tuple2<Rule, T>> assigner = new BoundedOutOfOrdernessTimestampExtractor<Tuple2<Rule, T>>(Time.milliseconds(100)) {
  @Override
  public long extractTimestamp(Tuple2<Rule, T> element) {
    return System.currentTimeMillis();
  }
};