Hướng dẫn exceljs number format - định dạng số exceljs

Cố gắng viết các số dòng tuần tự trong một ô như thế này: '0001'. Khi người dùng thêm một dòng và nhập số dòng '0002', Excel sẽ định dạng lại thành một số: 2

Show

Nếu tôi thay đổi định dạng số của ô thành văn bản (trong excel), nhập giá trị '0002' sẽ tồn tại.

Tôi đã cố gắng gán một định dạng số khi xác định cột:

    cell.numFmt = '0.00%' // convert `58` to `58.00%`
     // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
4
    cell.numFmt = '0.00%' // convert `58` to `58.00%`
     // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
4

Excel sẽ coi đây là một định dạng tùy chỉnh và khi bạn nhập '0002' vào ô, nó sẽ định dạng lại thành 't1900xt' với giá trị thanh công thức = '1/2/1900'

Đã thử

    cell.numFmt = '0.00%' // convert `58` to `58.00%`
     // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
5 cũng như không có sự khác biệt về kết quả.

Yêu thư viện này btw.

In my table the format of the numbers: "0.00", the same format is output to the worksheet, and I need to output in the excel "0,00" This is my structure `
This is my structure
`

    var body = [];
$(element).each(function(index_body, element_body) {
  $($(element_body).find("#tbody tr")).each(function(index_tr, element_tr) {
	$(this.cells).each(function(index_td, element_td) {
	  body.push($(element_td).text());
	});
	
	var row = worksheet.addRow(body);
	row.eachCell(function(cell) {
				
	cell.numFmt = '#,##';
	cell.fill = {
		   type: 'pattern',
		   pattern:'solid',
		   fgColor:{ argb:'deeded'}
		};
		
	  cell.font = { name: 'Verdana', size: 14};
	  cell.alignment = {horizontal: 'center' };
	  cell.border = {
		 top: { style: "thin" },
		 left: { style: "thin" },
		 bottom: { style: "thin" },
		 right: { style: "thin" }
		};
	});

	body = [];
  
	});
	});`

Thank you in advance for answers)

Xem thêm

Mô hình đối tượng JavaScript Excel trong các tiện ích bổ sung văn phòngShow

  • Mẫu mã sau đây nhận được phạm vi B2: E6, tải thuộc tính ____22 của nó và ghi nó vào bảng điều khiển. Thuộc tính
  •     cell.numFmt = '0.00%' // convert `58` to `58.00%`
         // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
    
    5 của nó và ghi nó vào bảng điều khiển.Thuộc tính
        cell.numFmt = '0.00%' // convert `58` to `58.00%`
         // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
    
    5 của một phạm vi chỉ định các công thức cho các ô trong phạm vi có chứa các công thức và các giá trị thô cho các ô trong phạm vi không chứa các công thức.B2:E6, loads its
        cell.numFmt = '0.00%' // convert `58` to `58.00%`
         // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
    
    5 property, and writes it to the console. The
        cell.numFmt = '0.00%' // convert `58` to `58.00%`
         // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
    
    5 property of a range specifies the formulas for cells in the range that contain formulas and the raw values for cells in the range that do not contain formulas.
    worksheet.columns.forEach(col => {
        col.eachCell(cell => {
            cell.value = +cell.value // convert a string value to a number value
        })
    })
    
    2
  • phạm vi.formulas (được ghi vào bảng điều khiển bằng mẫu mã ở trên)
  • Xem thêm
  • Mô hình đối tượng JavaScript Excel trong các tiện ích bổ sung văn phòng
  • Dữ liệu trước khi giá trị ô được cập nhật
  • Dữ liệu trước khi giá trị ô được cập nhật
  • Dữ liệu trước công thức ô được đặt
  • Nhận các giá trị từ một loạt các ô
  • Nhận văn bản từ một loạt các ô
  • Nhận văn bản từ một loạt các ô
  •     cell.numFmt = '0.00%' // convert `58` to `58.00%`
         // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
    
    5 của nó và ghi nó vào bảng điều khiển.Thuộc tính
        cell.numFmt = '0.00%' // convert `58` to `58.00%`
         // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
    
    5 của một phạm vi chỉ định các công thức cho các ô trong phạm vi có chứa các công thức và các giá trị thô cho các ô trong phạm vi không chứa các công thức.B2:E6, loads its
        cell.numFmt = '0.00%' // convert `58` to `58.00%`
         // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
    
    5 property, and writes it to the console. The
        cell.numFmt = '0.00%' // convert `58` to `58.00%`
         // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
    
    5 property of a range specifies the formulas for cells in the range that contain formulas and the raw values for cells in the range that do not contain formulas.
    worksheet.columns.forEach(col => {
        col.eachCell(cell => {
            cell.value = +cell.value // convert a string value to a number value
        })
    })
    
    2

const workbook = new Excel.Workbook();
const sheet = workbook.addWorksheet('My Sheet');
const rows = [
    [1, 'Bob', true],
    [2, 'Jake', false]
];

sheet.addRows(rows);

phạm vi.formulas (được ghi vào bảng điều khiển bằng mẫu mã ở trên)

Xem thêm
set the cell type automatically by exceljs, like

worksheet.columns.forEach(col => {
    col.eachCell(cell => {
        cell.value = +cell.value // convert a string value to a number value
    })
})

Mô hình đối tượng JavaScript Excel trong các tiện ích bổ sung văn phòng

    cell.numFmt = '0.00%' // convert `58` to `58.00%`
     // cell.numFmt = '#,##0' // convert '1234' to  `1,234`

Làm việc với các tế bào bằng cách sử dụng API JavaScript Excel

worksheet.getColumn(2).numFmt = '#,##0'

Mẫu mã sau đây nhận được phạm vi B2: E6, tải thuộc tính ____22 của nó và ghi nó vào bảng điều khiển. Thuộc tính

    cell.numFmt = '0.00%' // convert `58` to `58.00%`
     // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
2 của một phạm vi chỉ định các giá trị hiển thị cho các ô trong phạm vi. Ngay cả khi một số ô trong một phạm vi chứa các công thức, thuộc tính
    cell.numFmt = '0.00%' // convert `58` to `58.00%`
     // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
2 của phạm vi chỉ định các giá trị hiển thị cho các ô đó, chứ không phải bất kỳ công thức nào.B2:E6, loads its
    cell.numFmt = '0.00%' // convert `58` to `58.00%`
     // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
2 property, and writes it to the console. The
    cell.numFmt = '0.00%' // convert `58` to `58.00%`
     // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
2 property of a range specifies the display values for cells in the range. Even if some cells in a range contain formulas, the
    cell.numFmt = '0.00%' // convert `58` to `58.00%`
     // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
2 property of the range specifies the display values for those cells, not any of the formulas.
worksheet.columns.forEach(col => {
    col.eachCell(cell => {
        cell.value = +cell.value // convert a string value to a number value
    })
})
0

phạm vi.text (được đăng nhập vào bảng điều khiển bằng mẫu mã ở trên)

Mẫu mã sau đây nhận được phạm vi B2: E6, tải thuộc tính ____22 của nó và ghi nó vào bảng điều khiển. Thuộc tính

  •     cell.numFmt = '0.00%' // convert `58` to `58.00%`
         // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
    
    2 của một phạm vi chỉ định các giá trị hiển thị cho các ô trong phạm vi. Ngay cả khi một số ô trong một phạm vi chứa các công thức, thuộc tính
        cell.numFmt = '0.00%' // convert `58` to `58.00%`
         // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
    
    2 của phạm vi chỉ định các giá trị hiển thị cho các ô đó, chứ không phải bất kỳ công thức nào.B2:E6, loads its
        cell.numFmt = '0.00%' // convert `58` to `58.00%`
         // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
    
    2 property, and writes it to the console. The
        cell.numFmt = '0.00%' // convert `58` to `58.00%`
         // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
    
    2 property of a range specifies the display values for cells in the range. Even if some cells in a range contain formulas, the
        cell.numFmt = '0.00%' // convert `58` to `58.00%`
         // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
    
    2 property of the range specifies the display values for those cells, not any of the formulas.
    worksheet.columns.forEach(col => {
        col.eachCell(cell => {
            cell.value = +cell.value // convert a string value to a number value
        })
    })
    
    0
  • phạm vi.text (được đăng nhập vào bảng điều khiển bằng mẫu mã ở trên)
  • Nhận các công thức từ một loạt các ô

cell.numFmt = '0.00%' // convert `58` to `58.00%` // cell.numFmt = '#,##0' // convert '1234' to `1,234` 5 của nó và ghi nó vào bảng điều khiển.Thuộc tính cell.numFmt = '0.00%' // convert `58` to `58.00%` // cell.numFmt = '#,##0' // convert '1234' to `1,234` 5 của một phạm vi chỉ định các công thức cho các ô trong phạm vi có chứa các công thức và các giá trị thô cho các ô trong phạm vi không chứa các công thức.B2:E6, loads its cell.numFmt = '0.00%' // convert `58` to `58.00%` // cell.numFmt = '#,##0' // convert '1234' to `1,234` 5 property, and writes it to the console. The cell.numFmt = '0.00%' // convert `58` to `58.00%` // cell.numFmt = '#,##0' // convert '1234' to `1,234` 5 property of a range specifies the formulas for cells in the range that contain formulas and the raw values for cells in the range that do not contain formulas.worksheet.columns.forEach(col => { col.eachCell(cell => { cell.value = +cell.value // convert a string value to a number value }) }) 2

phạm vi.formulas (được ghi vào bảng điều khiển bằng mẫu mã ở trên)

Xem thêm

Bài báo

03/22/2022

4 phút để đọc

Trong bài viết này

Bài viết này cung cấp các mẫu mã đặt và nhận các giá trị, văn bản hoặc công thức phạm vi với API JavaScript Excel. Để biết danh sách đầy đủ các thuộc tính và phương thức mà đối tượng

worksheet.columns.forEach(col => {
    col.eachCell(cell => {
        cell.value = +cell.value // convert a string value to a number value
    })
})
5 hỗ trợ, xem lớp Excel.range.C3 to "5" and then sets the width of the columns to best fit the data.
await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");

    let range = sheet.getRange("C3");
    range.values = [[ 5 ]];
    range.format.autofitColumns();

    await context.sync();
});
C3 to "5" and then sets the width of the columns to best fit the data.
await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");

    let range = sheet.getRange("C3");
    range.values = [[ 5 ]];
    range.format.autofitColumns();

    await context.sync();
});

Ghi chú

API JavaScript Excel không có đối tượng hoặc lớp "ô". Thay vào đó, API JavaScript Excel xác định tất cả các ô Excel là đối tượng
worksheet.columns.forEach(col => {
    col.eachCell(cell => {
        cell.value = +cell.value // convert a string value to a number value
    })
})
5. Một ô riêng lẻ trong UI Excel chuyển thành đối tượng
worksheet.columns.forEach(col => {
    col.eachCell(cell => {
        cell.value = +cell.value // convert a string value to a number value
    })
})
5 với một ô trong API JavaScript Excel. Một đối tượng
worksheet.columns.forEach(col => {
    col.eachCell(cell => {
        cell.value = +cell.value // convert a string value to a number value
    })
})
5 cũng có thể chứa nhiều ô tiếp giáp. Xem công việc với các ô bằng API JavaScript Excel để tìm hiểu thêm.

Đặt các giá trị hoặc công thức

Các mẫu mã sau đây đặt các giá trị và công thức cho một ô hoặc một phạm vi ô.B5:D5 and then sets the width of the columns to best fit the data.B5:D5 and then sets the width of the columns to best fit the data.

const workbook = new Excel.Workbook();
const sheet = workbook.addWorksheet('My Sheet');
const rows = [
    [1, 'Bob', true],
    [2, 'Jake', false]
];

sheet.addRows(rows);
0

Đặt giá trị cho một ô đơn lẻ

Mẫu mã sau đây đặt giá trị của ô C3 thành "5" và sau đó đặt chiều rộng của các cột để phù hợp nhất với dữ liệu.

Dữ liệu trước khi giá trị ô được cập nhật

Dữ liệu sau giá trị ô được cập nhậtE3 and then sets the width of the columns to best fit the data.E3 and then sets the width of the columns to best fit the data.

const workbook = new Excel.Workbook();
const sheet = workbook.addWorksheet('My Sheet');
const rows = [
    [1, 'Bob', true],
    [2, 'Jake', false]
];

sheet.addRows(rows);
1

Đặt giá trị cho một loạt các ô

Mẫu mã sau đây đặt các giá trị cho các ô trong phạm vi B5: D5 và sau đó đặt chiều rộng của các cột để phù hợp nhất với dữ liệu.

Dữ liệu trước khi giá trị ô được cập nhật

Dữ liệu sau giá trị ô được cập nhậtE3 and then sets the width of the columns to best fit the data.E2:E6 and then sets the width of the columns to best fit the data.

const workbook = new Excel.Workbook();
const sheet = workbook.addWorksheet('My Sheet');
const rows = [
    [1, 'Bob', true],
    [2, 'Jake', false]
];

sheet.addRows(rows);
2

Đặt giá trị cho một loạt các ô

Mẫu mã sau đây đặt các giá trị cho các ô trong phạm vi B5: D5 và sau đó đặt chiều rộng của các cột để phù hợp nhất với dữ liệu.

Dữ liệu sau các giá trị ô được cập nhậtE2:E6 and then sets the width of the columns to best fit the data.

Đặt công thức cho một ô duy nhất

Mẫu mã sau đây đặt một công thức cho ô E3 và sau đó đặt chiều rộng của các cột để phù hợp nhất với dữ liệu.

Dữ liệu trước công thức ô được đặt

Dữ liệu sau công thức ô được đặtB2:E6, loads its
worksheet.columns.forEach(col => {
    col.eachCell(cell => {
        cell.value = +cell.value // convert a string value to a number value
    })
})
9 property, and writes the values to the console. The
worksheet.columns.forEach(col => {
    col.eachCell(cell => {
        cell.value = +cell.value // convert a string value to a number value
    })
})
9 property of a range specifies the raw values that the cells contain. Even if some cells in a range contain formulas, the
worksheet.columns.forEach(col => {
    col.eachCell(cell => {
        cell.value = +cell.value // convert a string value to a number value
    })
})
9 property of the range specifies the raw values for those cells, not any of the formulas.
const workbook = new Excel.Workbook();
const sheet = workbook.addWorksheet('My Sheet');
const rows = [
    [1, 'Bob', true],
    [2, 'Jake', false]
];

sheet.addRows(rows);
9

Nhận các giá trị từ một loạt các ô

Mẫu mã sau đây nhận được phạm vi B2: E6, tải thuộc tính

worksheet.columns.forEach(col => {
    col.eachCell(cell => {
        cell.value = +cell.value // convert a string value to a number value
    })
})
0

worksheet.columns.forEach(col => { col.eachCell(cell => { cell.value = +cell.value // convert a string value to a number value }) }) 9 của nó và ghi các giá trị vào bảng điều khiển. Thuộc tính worksheet.columns.forEach(col => { col.eachCell(cell => { cell.value = +cell.value // convert a string value to a number value }) }) 9 của một phạm vi chỉ định các giá trị thô mà các ô chứa. Ngay cả khi một số ô trong một phạm vi chứa các công thức, thuộc tính worksheet.columns.forEach(col => { col.eachCell(cell => { cell.value = +cell.value // convert a string value to a number value }) }) 9 của phạm vi chỉ định các giá trị thô cho các ô đó, chứ không phải bất kỳ công thức nào.B2:E6, loads its worksheet.columns.forEach(col => { col.eachCell(cell => { cell.value = +cell.value // convert a string value to a number value }) }) 9 property, and writes the values to the console. The worksheet.columns.forEach(col => { col.eachCell(cell => { cell.value = +cell.value // convert a string value to a number value }) }) 9 property of a range specifies the raw values that the cells contain. Even if some cells in a range contain formulas, the worksheet.columns.forEach(col => { col.eachCell(cell => { cell.value = +cell.value // convert a string value to a number value }) }) 9 property of the range specifies the raw values for those cells, not any of the formulas.const workbook = new Excel.Workbook(); const sheet = workbook.addWorksheet('My Sheet'); const rows = [ [1, 'Bob', true], [2, 'Jake', false] ]; sheet.addRows(rows); 9

Dữ liệu trong phạm vi (giá trị trong cột E là kết quả của các công thức)

phạm vi.values ​​(được ghi vào bảng điều khiển bằng mẫu mã ở trên)B2:E6, loads its
    cell.numFmt = '0.00%' // convert `58` to `58.00%`
     // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
2 property, and writes it to the console. The
    cell.numFmt = '0.00%' // convert `58` to `58.00%`
     // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
2 property of a range specifies the display values for cells in the range. Even if some cells in a range contain formulas, the
    cell.numFmt = '0.00%' // convert `58` to `58.00%`
     // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
2 property of the range specifies the display values for those cells, not any of the formulas.
worksheet.columns.forEach(col => {
    col.eachCell(cell => {
        cell.value = +cell.value // convert a string value to a number value
    })
})
0

Nhận các giá trị từ một loạt các ô

Mẫu mã sau đây nhận được phạm vi B2: E6, tải thuộc tính

worksheet.columns.forEach(col => {
    col.eachCell(cell => {
        cell.value = +cell.value // convert a string value to a number value
    })
})
1

worksheet.columns.forEach(col => { col.eachCell(cell => { cell.value = +cell.value // convert a string value to a number value }) }) 9 của nó và ghi các giá trị vào bảng điều khiển. Thuộc tính worksheet.columns.forEach(col => { col.eachCell(cell => { cell.value = +cell.value // convert a string value to a number value }) }) 9 của một phạm vi chỉ định các giá trị thô mà các ô chứa. Ngay cả khi một số ô trong một phạm vi chứa các công thức, thuộc tính worksheet.columns.forEach(col => { col.eachCell(cell => { cell.value = +cell.value // convert a string value to a number value }) }) 9 của phạm vi chỉ định các giá trị thô cho các ô đó, chứ không phải bất kỳ công thức nào.B2:E6, loads its worksheet.columns.forEach(col => { col.eachCell(cell => { cell.value = +cell.value // convert a string value to a number value }) }) 9 property, and writes the values to the console. The worksheet.columns.forEach(col => { col.eachCell(cell => { cell.value = +cell.value // convert a string value to a number value }) }) 9 property of a range specifies the raw values that the cells contain. Even if some cells in a range contain formulas, the worksheet.columns.forEach(col => { col.eachCell(cell => { cell.value = +cell.value // convert a string value to a number value }) }) 9 property of the range specifies the raw values for those cells, not any of the formulas.const workbook = new Excel.Workbook(); const sheet = workbook.addWorksheet('My Sheet'); const rows = [ [1, 'Bob', true], [2, 'Jake', false] ]; sheet.addRows(rows); 9

Dữ liệu trước công thức ô được đặt

Dữ liệu sau công thức ô được đặtB2:E6, loads its
    cell.numFmt = '0.00%' // convert `58` to `58.00%`
     // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
5 property, and writes it to the console. The
    cell.numFmt = '0.00%' // convert `58` to `58.00%`
     // cell.numFmt = '#,##0' // convert '1234' to  `1,234`
5 property of a range specifies the formulas for cells in the range that contain formulas and the raw values for cells in the range that do not contain formulas.
worksheet.columns.forEach(col => {
    col.eachCell(cell => {
        cell.value = +cell.value // convert a string value to a number value
    })
})
2

Nhận các giá trị từ một loạt các ô

Mẫu mã sau đây nhận được phạm vi B2: E6, tải thuộc tính

worksheet.columns.forEach(col => {
    col.eachCell(cell => {
        cell.value = +cell.value // convert a string value to a number value
    })
})
3

worksheet.columns.forEach(col => { col.eachCell(cell => { cell.value = +cell.value // convert a string value to a number value }) }) 9 của nó và ghi các giá trị vào bảng điều khiển. Thuộc tính worksheet.columns.forEach(col => { col.eachCell(cell => { cell.value = +cell.value // convert a string value to a number value }) }) 9 của một phạm vi chỉ định các giá trị thô mà các ô chứa. Ngay cả khi một số ô trong một phạm vi chứa các công thức, thuộc tính worksheet.columns.forEach(col => { col.eachCell(cell => { cell.value = +cell.value // convert a string value to a number value }) }) 9 của phạm vi chỉ định các giá trị thô cho các ô đó, chứ không phải bất kỳ công thức nào.B2:E6, loads its worksheet.columns.forEach(col => { col.eachCell(cell => { cell.value = +cell.value // convert a string value to a number value }) }) 9 property, and writes the values to the console. The worksheet.columns.forEach(col => { col.eachCell(cell => { cell.value = +cell.value // convert a string value to a number value }) }) 9 property of a range specifies the raw values that the cells contain. Even if some cells in a range contain formulas, the worksheet.columns.forEach(col => { col.eachCell(cell => { cell.value = +cell.value // convert a string value to a number value }) }) 9 property of the range specifies the raw values for those cells, not any of the formulas.const workbook = new Excel.Workbook(); const sheet = workbook.addWorksheet('My Sheet'); const rows = [ [1, 'Bob', true], [2, 'Jake', false] ]; sheet.addRows(rows); 9

  • Dữ liệu trong phạm vi (giá trị trong cột E là kết quả của các công thức)
  • phạm vi.values ​​(được ghi vào bảng điều khiển bằng mẫu mã ở trên)
  • Nhận văn bản từ một loạt các ô
  • Mẫu mã sau đây nhận được phạm vi B2: E6, tải thuộc tính ____22 của nó và ghi nó vào bảng điều khiển. Thuộc tính

cell.numFmt = '0.00%' // convert `58` to `58.00%` // cell.numFmt = '#,##0' // convert '1234' to `1,234` 2 của một phạm vi chỉ định các giá trị hiển thị cho các ô trong phạm vi. Ngay cả khi một số ô trong một phạm vi chứa các công thức, thuộc tính cell.numFmt = '0.00%' // convert `58` to `58.00%` // cell.numFmt = '#,##0' // convert '1234' to `1,234` 2 của phạm vi chỉ định các giá trị hiển thị cho các ô đó, chứ không phải bất kỳ công thức nào.B2:E6, loads its cell.numFmt = '0.00%' // convert `58` to `58.00%` // cell.numFmt = '#,##0' // convert '1234' to `1,234` 2 property, and writes it to the console. The cell.numFmt = '0.00%' // convert `58` to `58.00%` // cell.numFmt = '#,##0' // convert '1234' to `1,234` 2 property of a range specifies the display values for cells in the range. Even if some cells in a range contain formulas, the cell.numFmt = '0.00%' // convert `58` to `58.00%` // cell.numFmt = '#,##0' // convert '1234' to `1,234` 2 property of the range specifies the display values for those cells, not any of the formulas.worksheet.columns.forEach(col => { col.eachCell(cell => { cell.value = +cell.value // convert a string value to a number value }) }) 0

phạm vi.text (được đăng nhập vào bảng điều khiển bằng mẫu mã ở trên)