我有一個函式yahoofinance可以根據股票代碼查詢 Yahoo!Finance 的股票資料。在呼叫該函式之前,我需要對輸入進行一些修改。
我的作業表中的代碼采用以下形式[venue].[company],例如XNAS.GOOG(納斯達克的 Google)、XAMS.INGA(阿姆斯特丹的 ING 銀行)和XSWX.CRSP(瑞士交易所的 Crispr)。
我用于場地的代碼是國際代碼,但大多數 API 不使用這些相同的代碼。例如,阿姆斯特丹證券交易所的正式名稱是XAMS,但AMS在 Google API 和ASYahoo 上。所以我有一張清單Exchanges,列出了所有這些場所。當呼叫我的自定義yahoofinance函式時,它將標準的“XMAS.INGA”作為輸入,然后切斷場地 ( XAMS) 并查找 Yahoo 名稱 ( AS),然后傳遞INGA.AS給 Yahoo API:
=yahoofinance(index(split(A2, "."), 0, 2) & "." & vlookup(index(split(A2, "."), 0, 1), Exchanges!E:J, 6, false))
function yahoofinance(ticker) {
// send ticker to Yahoo API
}
現在在B2作業表中的單元格中的邏輯變得難以理解。如果可能的話,我想將它移動到函式中,并且只傳遞來自以下位置的代碼A2:
=yahoofinance(A2)
function yahoofinance(ticker) {
const venue = ticker.split();
// etc
// end up with INGA.AS, and pass to Yahoo API
}
我的問題是:是否可以將現在作業表中的邏輯移動到函式中?例如,如果以及如何vlookup從內部執行相同的操作yahoofinance?甚至可能嗎?
我正計劃真正擴展 的功能yahoofinance,甚至可能將其重寫為一個通用finance函式,該函式也將作為引數傳遞給您想要傳遞代碼的 API:,=yahoofinance(ticker, api)因此非常感謝任何幫助我在這里打下堅實基礎的幫助。
uj5u.com熱心網友回復:
您可以嘗試使用此自定義公式來獲取修改后的值:
腳本:
function yahoofinance(ticker) {
var [venue, company] = ticker.split("\.");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Exchanges');
var lastRow = sheet.getLastRow();
// get Exchanges!E:J values
var mapping = sheet.getRange(1, 5, lastRow, 6).getValues();
// lookup equivalent, filter then get 6th column and append to company
var modifiedTicker = company '.' mapping.filter(row => row[0] == venue).flat()[5];
// you now have the modified ticker. Use it on your function.
// ...
return modifiedTicker;
}
B7公式:
=index(split(A2, "."), 0, 2) & "." & vlookup(index(split(A2, "."), 0, 1), Exchanges!E:J, 6, false)
輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/464919.html
