Sheets API v4 返回特定公式的#N/A

Sheets API v4 is returning #N/A for specific formulas

Sheets API v4 在使用 vlookup 公式时返回 #N/A。 vlookup 公式会发生此特定错误,但我在包含大量公式的电子表格中看到了同样的问题。似乎 API 在返回值之前不等待电子表格计算公式。

python中的一个小代码示例:

service.spreadsheets().values().get(
    spreadsheetId='1s-fePyrjzp8_jAWy-p1smystgJXxMirmjXGJjQM91qg',
    range="FxRates salaries filter!A1:D5",
    valueRenderOption="UNFORMATTED_VALUE",
    dateTimeRenderOption="SERIAL_NUMBER"
).execute()

步骤是否会重现问题?

  1. 在 Google 电子表格上创建 vlookup 公式
  2. 使用表格API得到公式的结果

预期输出: 预期的输出是一个数字,但 API 返回“#N/A(在 VLOOKUP 评估中未找到值‘43531’。)”。电子表格本身可以正确计算公式,但是 API returns #N/A

您可以通过执行下面的 link 在 API Explorer 上轻松重现该问题: Google Sheets API Explorer

当我检查 FxRates salaries filter!A1:D5 的公式时,检索到以下值。

{
  "range": "'FxRates salaries filter'!A1:D5",
  "majorDimension": "ROWS",
  "values": [
    [
      "Date",
      "EUR",
      "USD",
      "BRL"
    ],
    [
      "=TODAY()-1",
      1,
      "=vlookup(A2,EURUSD_D!$A:$C,3,0)",
      "=vlookup(A2,EURBRL_D!$A:$C,3,0)"
    ]
  ]
}

当我检查 EURUSD_D!$A:$C 的公式时,检索到以下值。 GOOGLEFINANCE() 检索到的值正在使用。 EURBRL_D!$A:$C 的值也是相同的结果。

{
  "range": "EURUSD_D!A1:C2258",
  "majorDimension": "ROWS",
  "values": [
    [
      "Date ONLY",
      "=GOOGLEFINANCE(\"CURRENCY:EURUSD\",\"price\",date(2014,1,2),today())"
    ],
    [
      "=ARRAYFORMULA(IF(B2:B=\"\",\"\",DATE(YEAR(B2:B),MONTH(B2:B),DAY(B2:B))))"
    ]
  ]
}

从以上结果来看,您的问题原因是“Historical GOOGLEFINANCE data no longer accessible outside of Google Sheets”。

Historical GOOGLEFINANCE data no longer accessible outside of Google Sheets

September 27, 2016

We want to make you aware of a small change to the GOOGLEFINANCE function, which makes it easy to pull current or historical securities information directly into your spreadsheets in Google Sheets. Starting today, it will not be possible to download historical data or access it outside of Sheets using Apps Script or an API. If you have a spreadsheet with historical data generated from the GOOGLEFINANCE function and you try to download it or access it via Apps Script or an API, the corresponding cells will show “#N/A.” You will still be able to view that historical data from within the Sheets application, and you will still be able to download current data and access current data via Apps Script or an API. Please keep this in mind when using the GOOGLEFINANCE function going forward.