Hämta cellsträngvärde med och utan formatering
Aspose.Cells tillhandahåller en metod Cell.getStringValue() som kan användas för att få cellens strängvärde med eller utan formatering. Anta att du har en cell med värdet 0.012345 och att du har format den för att visa endast två decimaler. Då visas den som 0.01 i Excel. Du kan hämta strängvärden både som 0.01 och som 0.012345 med hjälp av Cell.getStringValue()-metoden. Den tar enum CellValueFormatStrategy som parameter med följande värden.
- CellValueFormatStrategy.CellStyle
- CellValueFormatStrategy.DisplayStyle
- CellValueFormatStrategy.DisplayString
- CellValueFormatStrategy.None
Följande kodexempel förklarar användningen av Cell.getStringValue()-metoden.
const AsposeCells = require("aspose.cells.node"); | |
const path = require("path"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create workbook | |
const workbook = new AsposeCells.Workbook(); | |
// Access first worksheet | |
const worksheet = workbook.getWorksheets().get(0); | |
// Access cell A1 | |
const cell = worksheet.getCells().get("A1"); | |
// Put value inside the cell | |
cell.putValue(0.012345); | |
// Format the cell that it should display 0.01 instead of 0.012345 | |
const style = cell.getStyle(); | |
style.setNumber(2); | |
cell.setStyle(style); | |
// Get string value as Cell Style | |
let value = cell.getStringValue(AsposeCells.CellValueFormatStrategy.CellStyle); | |
console.log(value); | |
// Get string value without any formatting | |
value = cell.getStringValue(AsposeCells.CellValueFormatStrategy.None); | |
console.log(value); |