【发布时间】:2021-10-10 09:08:40
【问题描述】:
我找到了一个从硬币市值下载价格的脚本,但在脚本中,您必须在脚本中单独输入硬币的名称。由于我可能想经常更改名称,因此从列中的列表中“获取”名称似乎会更容易。 在下面的代码中,您在第 21 行输入每个名称。有没有办法将代码编辑为 B2:B10 中列出的任何名称,并将这些名称输入到脚本中?所以我可以只编辑 B 列而不是脚本?
提前致谢
enter codefunction coin_price() {
const myGoogleSheetName =
// Change "Sheet1" to the name of your sheet where you want to run this.
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1')
// Call CoinMarketCap and let them know who you are.
const coinMarketCapAPICall = {
method: 'GET',
uri: 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest',
qs: {
start: '1',
limit: '5000',
convert: 'USD',
},
headers: { 'X-CMC_PRO_API_KEY': 'YOUR_API_KEY_GOES_HERE' },
json: true,
gzip: true,
}
// Put the coin symbols that you want to follow here.
const myCoinSymbols = ['BTC', 'ETH', 'FIL', 'BTT', 'AMP', 'DOT']
// Let's itereate
for (let i = 0; i < myCoinSymbols.length; i++) {
const coinSymbol = myCoinSymbols[i]
const coinMarketCapUrl = `https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=${coinSymbol}`
const result = UrlFetchApp.fetch(coinMarketCapUrl, coinMarketCapAPICall)
const txt = result.getContentText()
const d = JSON.parse(txt)
const row = i + 2
// Puts a column of at symbols into the sheet at B2.
myGoogleSheetName.getRange(row, 2).setValue(coinSymbol)
// Puts a column of current market price's in dollars into the sheet at B3.
myGoogleSheetName
.getRange(row, 3)
.setValue(d.data[coinSymbol].quote.USD.price)
【问题讨论】:
标签: google-apps-script google-sheets google-sheets-formula