這個問題在這里已經有了答案: google 應用程式腳本上 getValue() 和 getDisplayValue() 之間的區別 (3 個回答) 21 小時前關閉。
在 ma Google Application Script 專案中,我需要確保始終將單元格資料作為文本讀取。為了確保它像我.setNumberFormat('@STRING@')在每個.appendRow(). 為了 100% 確定我什至在更新單元格后更改格式。
但碰巧在電子表格中間有一排格式不是文本而是......我猜它沒有格式。
因此,在讀取和使用此類資料時會產生錯誤 Thu Mar 11 2021 00:00:00 GMT 0100 (Central European Standard Time) Error TypeError: data[row][1].split is not a function at getTabulatorData(web functions:445:34)
因為 split() 函式不適用于日期物件。
單元格中保存的資料是 3/11/2021
當前獲取資料的代碼是
var range = sheetData.getRange(firstRow, 1, lastRow, columns)
var data = range.getValues()
如果我使用
var dataTMP = Sheets.Spreadsheets.Values.get(spreadsheetId, sheetName "!" range.getA1Notation(),
{valueRenderOption:"FORMATTED_VALUE"})
然后我得到正確的文本值,即使我將單元格格式化為日期。
這是將資料作為文本讀取的正確方法嗎?在 Google檔案中是這樣寫的
// This code uses the Sheets Advanced Service, but for most use cases
// the built-in method SpreadsheetApp.getActiveSpreadsheet()
// .getRange(range).getValues(values) is more appropriate.
var result = Sheets.Spreadsheets.Values.get(spreadsheetId, range);
var numRows = result.values ? result.values.length : 0;
問題 1:是否Sheets.Spreadsheets.Values.get(spreadsheetId,sheetName "!" range.getA1Notation(),{valueRenderOption:"FORMATTED_VALUE"})給我總是將資料保存為原始文本?
問題2:有沒有辦法 .getRange().getValues()讀取、解釋保存的資料?
uj5u.com熱心網友回復:
嘗試使用.getDisplayValues()而不是 .getValues()。第一個將以字串形式回傳值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/380437.html
標籤:谷歌应用程序脚本 谷歌表格 google-sheets-api
上一篇:使用ischecked函式激活宏
