Hướng dẫn how to connect javascript with excel sheet? - làm thế nào để kết nối javascript với trang tính excel?

Bỏ qua nội dung chính

Trình duyệt này không còn được hỗ trợ.

Show

Nâng cấp lên Microsoft Edge để tận dụng các tính năng mới nhất, cập nhật bảo mật và hỗ trợ kỹ thuật.

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

  • Bài báo
  • 07/21/2022
  • 15 phút để đọc

Trong bài viết này

Bài viết này cung cấp các mẫu mã cho thấy cách thực hiện các tác vụ phổ biến với bảng tính bằng cách sử dụng API JavaScript Excel. Để biết danh sách đầy đủ các thuộc tính và phương thức mà các đối tượng

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    sheet.activate();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
7 và
await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    sheet.activate();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
8 hỗ trợ, hãy xem đối tượng bảng tính (API JavaScript cho Excel) và đối tượng bảng tính (API JavaScript cho Excel).

Ghi chú

Thông tin trong bài viết này chỉ áp dụng cho bảng tính thông thường; Nó không áp dụng cho các tấm "biểu đồ" hoặc các tấm "macro".

Nhận bảng tính

Mẫu mã sau đây nhận được bộ sưu tập các bảng tính, tải thuộc tính

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    sheet.activate();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
9 của mỗi bảng tính và viết một tin nhắn vào bảng điều khiển.

await Excel.run(async (context) => {
    let sheets = context.workbook.worksheets;
    sheets.load("items/name");

    await context.sync();
    
    if (sheets.items.length > 1) {
        console.log(`There are ${sheets.items.length} worksheets in the workbook:`);
    } else {
        console.log(`There is one worksheet in the workbook:`);
    }

    sheets.items.forEach(function (sheet) {
        console.log(sheet.name);
    });
});

Ghi chú

Thông tin trong bài viết này chỉ áp dụng cho bảng tính thông thường; Nó không áp dụng cho các tấm "biểu đồ" hoặc các tấm "macro".

Nhận bảng tính

Mẫu mã sau đây nhận được bộ sưu tập các bảng tính, tải thuộc tính

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    sheet.activate();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
9 của mỗi bảng tính và viết một tin nhắn vào bảng điều khiển.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});

Thuộc tính await Excel.run(async (context) => { let firstSheet = context.workbook.worksheets.getFirst(); firstSheet.load("name"); await context.sync(); console.log(`The name of the first worksheet is "${firstSheet.name}"`); }); 0 của bảng tính xác định duy nhất bảng tính trong một sổ làm việc nhất định và giá trị của nó sẽ vẫn giữ nguyên ngay cả khi bảng tính được đổi tên hoặc di chuyển. Khi một bảng tính bị xóa khỏi sổ làm việc ở Excel trên Mac, await Excel.run(async (context) => { let firstSheet = context.workbook.worksheets.getFirst(); firstSheet.load("name"); await context.sync(); console.log(`The name of the first worksheet is "${firstSheet.name}"`); }); 0 của bảng tính bị xóa có thể được chỉ định lại vào một bảng tính mới được tạo sau đó.

Nhận bảng tính tích cựcSample, loads its

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    sheet.activate();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
9 property, and writes a message to the console. If there is no worksheet with that name, the
await Excel.run(async (context) => {
    let firstSheet = context.workbook.worksheets.getFirst();
    firstSheet.load("name");

    await context.sync();
    console.log(`The name of the first worksheet is "${firstSheet.name}"`);
});
4 method throws an
await Excel.run(async (context) => {
    let firstSheet = context.workbook.worksheets.getFirst();
    firstSheet.load("name");

    await context.sync();
    console.log(`The name of the first worksheet is "${firstSheet.name}"`);
});
5 error.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    sheet.activate();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});

Mẫu mã sau đây nhận được bảng tính hoạt động, tải thuộc tính await Excel.run(async (context) => { let sheet = context.workbook.worksheets.getItem("Sample"); sheet.activate(); sheet.load("name"); await context.sync(); console.log(`The active worksheet is "${sheet.name}"`); }); 9 của nó và ghi một thông báo vào bảng điều khiển.

Đặt bảng tính hoạt động

Mẫu mã sau đây đặt bảng tính hoạt động vào bảng tính có tên mẫu, tải thuộc tính await Excel.run(async (context) => { let sheet = context.workbook.worksheets.getItem("Sample"); sheet.activate(); sheet.load("name"); await context.sync(); console.log(`The active worksheet is "${sheet.name}"`); }); 9 của nó và ghi tin nhắn vào bảng điều khiển. Nếu không có bảng tính với tên đó, phương thức await Excel.run(async (context) => { let firstSheet = context.workbook.worksheets.getFirst(); firstSheet.load("name"); await context.sync(); console.log(`The name of the first worksheet is "${firstSheet.name}"`); }); 4 đã gây ra lỗi await Excel.run(async (context) => { let firstSheet = context.workbook.worksheets.getFirst(); firstSheet.load("name"); await context.sync(); console.log(`The name of the first worksheet is "${firstSheet.name}"`); }); 5.

Bảng tính tham chiếu theo vị trí tương đối

await Excel.run(async (context) => {
    let firstSheet = context.workbook.worksheets.getFirst();
    firstSheet.load("name");

    await context.sync();
    console.log(`The name of the first worksheet is "${firstSheet.name}"`);
});

Những ví dụ này cho thấy cách tham khảo một bảng tính theo vị trí tương đối của nó.

Nhận bảng tính đầu tiên

await Excel.run(async (context) => {
    let lastSheet = context.workbook.worksheets.getLast();
    lastSheet.load("name");

    await context.sync();
    console.log(`The name of the last worksheet is "${lastSheet.name}"`);
});

Mẫu mã sau đây nhận được bảng tính đầu tiên trong sổ làm việc, tải thuộc tính await Excel.run(async (context) => { let sheet = context.workbook.worksheets.getItem("Sample"); sheet.activate(); sheet.load("name"); await context.sync(); console.log(`The active worksheet is "${sheet.name}"`); }); 9 của nó và viết một tin nhắn vào bảng điều khiển.

Nhận bảng tính cuối cùng

await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let nextSheet = currentSheet.getNext();
    nextSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that follows the active worksheet is "${nextSheet.name}"`);
});

Mẫu mã sau đây nhận được bảng tính cuối cùng trong sổ làm việc, tải thuộc tính await Excel.run(async (context) => { let sheet = context.workbook.worksheets.getItem("Sample"); sheet.activate(); sheet.load("name"); await context.sync(); console.log(`The active worksheet is "${sheet.name}"`); }); 9 của nó và viết một tin nhắn vào bảng điều khiển.

Nhận bảng tính tiếp theo

await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let previousSheet = currentSheet.getPrevious();
    previousSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that precedes the active worksheet is "${previousSheet.name}"`);
});

Mẫu mã sau đây nhận được bảng tính theo bảng tính hoạt động trong sổ làm việc, tải thuộc tính await Excel.run(async (context) => { let sheet = context.workbook.worksheets.getItem("Sample"); sheet.activate(); sheet.load("name"); await context.sync(); console.log(`The active worksheet is "${sheet.name}"`); }); 9 của nó và viết một thông báo vào bảng điều khiển. Nếu không có bảng tính sau bảng tính đang hoạt động, phương thức await Excel.run(async (context) => { let firstSheet = context.workbook.worksheets.getFirst(); firstSheet.load("name"); await context.sync(); console.log(`The name of the first worksheet is "${firstSheet.name}"`); }); 9 đã gây ra lỗi await Excel.run(async (context) => { let firstSheet = context.workbook.worksheets.getFirst(); firstSheet.load("name"); await context.sync(); console.log(`The name of the first worksheet is "${firstSheet.name}"`); }); 5.

Nhận bảng tính trước đóSample to the workbook, loads its

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    sheet.activate();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
9 and
await Excel.run(async (context) => {
    let lastSheet = context.workbook.worksheets.getLast();
    lastSheet.load("name");

    await context.sync();
    console.log(`The name of the last worksheet is "${lastSheet.name}"`);
});
5 properties, and writes a message to the console. The new worksheet is added after all existing worksheets.

await Excel.run(async (context) => {
    let sheets = context.workbook.worksheets;

    let sheet = sheets.add("Sample");
    sheet.load("name, position");

    await context.sync();
    console.log(`Added worksheet named "${sheet.name}" in position ${sheet.position}`);
});

Mẫu mã sau đây nhận được bảng tính trước bảng tính hoạt động trong sổ làm việc, tải thuộc tính await Excel.run(async (context) => { let sheet = context.workbook.worksheets.getItem("Sample"); sheet.activate(); sheet.load("name"); await context.sync(); console.log(`The active worksheet is "${sheet.name}"`); }); 9 của nó và viết một thông báo vào bảng điều khiển. Nếu không có bảng tính trước bảng tính hoạt động, phương thức await Excel.run(async (context) => { let lastSheet = context.workbook.worksheets.getLast(); lastSheet.load("name"); await context.sync(); console.log(`The name of the last worksheet is "${lastSheet.name}"`); }); 2 đã gây ra lỗi await Excel.run(async (context) => { let firstSheet = context.workbook.worksheets.getFirst(); firstSheet.load("name"); await context.sync(); console.log(`The name of the first worksheet is "${firstSheet.name}"`); }); 5.

Thêm một bảng tínhMySheet (2)).

await Excel.run(async (context) => {
    let lastSheet = context.workbook.worksheets.getLast();
    lastSheet.load("name");

    await context.sync();
    console.log(`The name of the last worksheet is "${lastSheet.name}"`);
});
6 can take two parameters, both of which are optional:

  • Mẫu mã sau đây thêm một bảng tính mới có tên mẫu vào sổ làm việc, tải các thuộc tính
    await Excel.run(async (context) => {
        let sheet = context.workbook.worksheets.getItem("Sample");
        sheet.activate();
        sheet.load("name");
    
        await context.sync();
        console.log(`The active worksheet is "${sheet.name}"`);
    });
    
    9 và
    await Excel.run(async (context) => {
        let lastSheet = context.workbook.worksheets.getLast();
        lastSheet.load("name");
    
        await context.sync();
        console.log(`The name of the last worksheet is "${lastSheet.name}"`);
    });
    
    5 của nó và ghi một thông báo vào bảng điều khiển. Bảng tính mới được thêm vào sau tất cả các bảng tính hiện có.
  • Sao chép một bảng tính hiện có

await Excel.run(async (context) => {
    let lastSheet = context.workbook.worksheets.getLast();
    lastSheet.load("name");

    await context.sync();
    console.log(`The name of the last worksheet is "${lastSheet.name}"`);
});
6 thêm một bảng tính mới là bản sao của một bảng tính hiện có. Tên của bảng tính mới sẽ có một số được thêm vào cuối, theo cách phù hợp với việc sao chép bảng tính thông qua UI Excel (ví dụ: MySheet (2)).
await Excel.run(async (context) => {
    let lastSheet = context.workbook.worksheets.getLast();
    lastSheet.load("name");

    await context.sync();
    console.log(`The name of the last worksheet is "${lastSheet.name}"`);
});
6 có thể lấy hai tham số, cả hai đều là tùy chọn:

await Excel.run(async (context) => {
    let myWorkbook = context.workbook;
    let sampleSheet = myWorkbook.worksheets.getActiveWorksheet();
    let copiedSheet = sampleSheet.copy(Excel.WorksheetPositionType.after, sampleSheet);
    await context.sync();
});

await Excel.run(async (context) => { let lastSheet = context.workbook.worksheets.getLast(); lastSheet.load("name"); await context.sync(); console.log(`The name of the last worksheet is "${lastSheet.name}"`); }); 8 - Một bảng tính enum chỉ định nơi trong sổ làm việc mà bảng tính mới sẽ được thêm vào.

await Excel.run(async (context) => {
    let lastSheet = context.workbook.worksheets.getLast();
    lastSheet.load("name");

    await context.sync();
    console.log(`The name of the last worksheet is "${lastSheet.name}"`);
});
9 - Nếu
await Excel.run(async (context) => {
    let lastSheet = context.workbook.worksheets.getLast();
    lastSheet.load("name");

    await context.sync();
    console.log(`The name of the last worksheet is "${lastSheet.name}"`);
});
8 là
await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let nextSheet = currentSheet.getNext();
    nextSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that follows the active worksheet is "${nextSheet.name}"`);
});
1 hoặc
await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let nextSheet = currentSheet.getNext();
    nextSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that follows the active worksheet is "${nextSheet.name}"`);
});
2, bạn cần chỉ định một bảng tính liên quan đến bảng mới sẽ được thêm vào (tham số này trả lời câu hỏi "trước hoặc sau cái gì?").

await Excel.run(async (context) => {
    let sheets = context.workbook.worksheets;
    sheets.load("items/name");

    await context.sync();
    if (sheets.items.length === 1) {
        console.log("Unable to delete the only worksheet in the workbook");
    } else {
        let lastSheet = sheets.items[sheets.items.length - 1];

        console.log(`Deleting worksheet named "${lastSheet.name}"`);
        lastSheet.delete();

        await context.sync();
    }
});

Ghi chú

Thông tin trong bài viết này chỉ áp dụng cho bảng tính thông thường; Nó không áp dụng cho các tấm "biểu đồ" hoặc các tấm "macro".

Nhận bảng tính

Mẫu mã sau đây nhận được bộ sưu tập các bảng tính, tải thuộc tính

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    sheet.activate();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
9 của mỗi bảng tính và viết một tin nhắn vào bảng điều khiển.New Name.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
0

Thuộc tính await Excel.run(async (context) => { let firstSheet = context.workbook.worksheets.getFirst(); firstSheet.load("name"); await context.sync(); console.log(`The name of the first worksheet is "${firstSheet.name}"`); }); 0 của bảng tính xác định duy nhất bảng tính trong một sổ làm việc nhất định và giá trị của nó sẽ vẫn giữ nguyên ngay cả khi bảng tính được đổi tên hoặc di chuyển. Khi một bảng tính bị xóa khỏi sổ làm việc ở Excel trên Mac, await Excel.run(async (context) => { let firstSheet = context.workbook.worksheets.getFirst(); firstSheet.load("name"); await context.sync(); console.log(`The name of the first worksheet is "${firstSheet.name}"`); }); 0 của bảng tính bị xóa có thể được chỉ định lại vào một bảng tính mới được tạo sau đó.

Nhận bảng tính tích cực

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
1

Mẫu mã sau đây nhận được bảng tính hoạt động, tải thuộc tính await Excel.run(async (context) => { let sheet = context.workbook.worksheets.getItem("Sample"); sheet.activate(); sheet.load("name"); await context.sync(); console.log(`The active worksheet is "${sheet.name}"`); }); 9 của nó và ghi một thông báo vào bảng điều khiển.

Đặt bảng tính hoạt động

Mẫu mã sau đây đặt bảng tính hoạt động vào bảng tính có tên mẫu, tải thuộc tính await Excel.run(async (context) => { let sheet = context.workbook.worksheets.getItem("Sample"); sheet.activate(); sheet.load("name"); await context.sync(); console.log(`The active worksheet is "${sheet.name}"`); }); 9 của nó và ghi tin nhắn vào bảng điều khiển. Nếu không có bảng tính với tên đó, phương thức await Excel.run(async (context) => { let firstSheet = context.workbook.worksheets.getFirst(); firstSheet.load("name"); await context.sync(); console.log(`The name of the first worksheet is "${firstSheet.name}"`); }); 4 đã gây ra lỗi await Excel.run(async (context) => { let firstSheet = context.workbook.worksheets.getFirst(); firstSheet.load("name"); await context.sync(); console.log(`The name of the first worksheet is "${firstSheet.name}"`); }); 5.

Bảng tính tham chiếu theo vị trí tương đốiSample to hidden, loads its

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    sheet.activate();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
9 property, and writes a message to the console.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
2

Những ví dụ này cho thấy cách tham khảo một bảng tính theo vị trí tương đối của nó.

Nhận bảng tính đầu tiênSample to visible, loads its

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    sheet.activate();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
9 property, and writes a message to the console.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
3

Nhận một ô duy nhất trong một bảng tính

Mẫu mã sau đây có được ô nằm trong hàng 2, cột 5 của bảng tính có tên mẫu, tải các thuộc tính

await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let nextSheet = currentSheet.getNext();
    nextSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that follows the active worksheet is "${nextSheet.name}"`);
});
6 và
await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let nextSheet = currentSheet.getNext();
    nextSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that follows the active worksheet is "${nextSheet.name}"`);
});
7 của nó và ghi một thông báo vào bảng điều khiển. Các giá trị được truyền vào phương thức
await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let nextSheet = currentSheet.getNext();
    nextSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that follows the active worksheet is "${nextSheet.name}"`);
});
8 là số hàng và số cột được chỉ số bằng không cho ô đang được truy xuất.Sample, loads its
await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let nextSheet = currentSheet.getNext();
    nextSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that follows the active worksheet is "${nextSheet.name}"`);
});
6 and
await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let nextSheet = currentSheet.getNext();
    nextSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that follows the active worksheet is "${nextSheet.name}"`);
});
7 properties, and writes a message to the console. The values that are passed into the
await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let nextSheet = currentSheet.getNext();
    nextSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that follows the active worksheet is "${nextSheet.name}"`);
});
8 method are the zero-indexed row number and column number for the cell that is being retrieved.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
4

Phát hiện thay đổi dữ liệu

Bổ trợ của bạn có thể cần phản ứng với người dùng thay đổi dữ liệu trong bảng tính. Để phát hiện những thay đổi này, bạn có thể đăng ký một trình xử lý sự kiện cho sự kiện

await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let nextSheet = currentSheet.getNext();
    nextSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that follows the active worksheet is "${nextSheet.name}"`);
});
9 của bảng tính. Trình xử lý sự kiện cho sự kiện
await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let nextSheet = currentSheet.getNext();
    nextSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that follows the active worksheet is "${nextSheet.name}"`);
});
9 nhận được một đối tượng WorkSheetchangedEventArgs khi sự kiện bị bắn.

Đối tượng

await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let previousSheet = currentSheet.getPrevious();
    previousSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that precedes the active worksheet is "${previousSheet.name}"`);
});
1 cung cấp thông tin về các thay đổi và nguồn. Vì
await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let nextSheet = currentSheet.getNext();
    nextSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that follows the active worksheet is "${nextSheet.name}"`);
});
9 bắn khi định dạng hoặc giá trị của các thay đổi dữ liệu, nên có thể hữu ích khi kiểm tra bổ trợ của bạn nếu các giá trị đã thực sự thay đổi. Tài sản
await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let previousSheet = currentSheet.getPrevious();
    previousSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that precedes the active worksheet is "${previousSheet.name}"`);
});
3 gói gọn thông tin này như một thay đổi. Mẫu mã sau đây cho thấy cách hiển thị các giá trị trước và sau và các loại ô đã được thay đổi.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
5

Phát hiện thay đổi công thức

Bổ trợ của bạn có thể theo dõi các thay đổi thành công thức trong bảng tính. Điều này rất hữu ích khi một bảng tính được kết nối với cơ sở dữ liệu bên ngoài. Khi công thức thay đổi trong bảng tính, sự kiện trong kịch bản này kích hoạt các bản cập nhật tương ứng trong cơ sở dữ liệu bên ngoài.

Để phát hiện các thay đổi đối với các công thức, hãy đăng ký một trình xử lý sự kiện cho sự kiện onformulachanged của một bảng tính. Trình xử lý sự kiện cho sự kiện

await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let previousSheet = currentSheet.getPrevious();
    previousSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that precedes the active worksheet is "${previousSheet.name}"`);
});
4 nhận được một đối tượng WorkSheetFormulachangedEventArgs khi sự kiện nổ ra.

Quan trọng

Sự kiện

await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let previousSheet = currentSheet.getPrevious();
    previousSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that precedes the active worksheet is "${previousSheet.name}"`);
});
4 phát hiện khi một công thức tự thay đổi, không phải giá trị dữ liệu do tính toán của công thức.

Mẫu mã sau đây cho thấy cách đăng ký trình xử lý sự kiện

await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let previousSheet = currentSheet.getPrevious();
    previousSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that precedes the active worksheet is "${previousSheet.name}"`);
});
4, sử dụng đối tượng
await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let previousSheet = currentSheet.getPrevious();
    previousSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that precedes the active worksheet is "${previousSheet.name}"`);
});
7 để truy xuất mảng formulAdetails của công thức đã thay đổi và sau đó in ra chi tiết về công thức đã thay đổi với các thuộc tính FormulachAchAtedEventDetail.

Ghi chú

Mẫu mã này chỉ hoạt động khi một công thức duy nhất được thay đổi.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
6

Xử lý các sự kiện sắp xếp

Các sự kiện

await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let previousSheet = currentSheet.getPrevious();
    previousSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that precedes the active worksheet is "${previousSheet.name}"`);
});
8 và
await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let previousSheet = currentSheet.getPrevious();
    previousSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that precedes the active worksheet is "${previousSheet.name}"`);
});
9 cho biết khi nào bất kỳ dữ liệu bảng tính nào được sắp xếp. Các sự kiện này được kết nối với các đối tượng
await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    sheet.activate();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
7 riêng lẻ và với
await Excel.run(async (context) => {
    let sheets = context.workbook.worksheets;

    let sheet = sheets.add("Sample");
    sheet.load("name, position");

    await context.sync();
    console.log(`Added worksheet named "${sheet.name}" in position ${sheet.position}`);
});
1 của sổ làm việc. Họ bắn cho dù việc sắp xếp được thực hiện theo chương trình hoặc thủ công thông qua giao diện người dùng Excel.

Ghi chú

Mẫu mã này chỉ hoạt động khi một công thức duy nhất được thay đổi.

Xử lý các sự kiện sắp xếp

Các sự kiện

await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let previousSheet = currentSheet.getPrevious();
    previousSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that precedes the active worksheet is "${previousSheet.name}"`);
});
8 và
await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let previousSheet = currentSheet.getPrevious();
    previousSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that precedes the active worksheet is "${previousSheet.name}"`);
});
9 cho biết khi nào bất kỳ dữ liệu bảng tính nào được sắp xếp. Các sự kiện này được kết nối với các đối tượng
await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    sheet.activate();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
7 riêng lẻ và với
await Excel.run(async (context) => {
    let sheets = context.workbook.worksheets;

    let sheet = sheets.add("Sample");
    sheet.load("name, position");

    await context.sync();
    console.log(`Added worksheet named "${sheet.name}" in position ${sheet.position}`);
});
1 của sổ làm việc. Họ bắn cho dù việc sắp xếp được thực hiện theo chương trình hoặc thủ công thông qua giao diện người dùng Excel.

Hướng dẫn how to connect javascript with excel sheet? - làm thế nào để kết nối javascript với trang tính excel?

await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let previousSheet = currentSheet.getPrevious();
    previousSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that precedes the active worksheet is "${previousSheet.name}"`);
});
8 Hỏa hoạn khi các cột được sắp xếp là kết quả của hoạt động sắp xếp từ trái sang phải.
await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let previousSheet = currentSheet.getPrevious();
    previousSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that precedes the active worksheet is "${previousSheet.name}"`);
});
9 Hỏa hoạn khi các hàng được sắp xếp là kết quả của hoạt động sắp xếp từ trên xuống dưới. Sắp xếp một bảng bằng menu thả xuống trên tiêu đề cột dẫn đến một sự kiện
await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let previousSheet = currentSheet.getPrevious();
    previousSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that precedes the active worksheet is "${previousSheet.name}"`);
});
9. Sự kiện tương ứng với những gì đang di chuyển, không phải là những gì đang được coi là tiêu chí sắp xếp.Q1" (the values in "B"), the following highlighted rows are returned by
await Excel.run(async (context) => {
    let myWorkbook = context.workbook;
    let sampleSheet = myWorkbook.worksheets.getActiveWorksheet();
    let copiedSheet = sampleSheet.copy(Excel.WorksheetPositionType.after, sampleSheet);
    await context.sync();
});
0.

Hướng dẫn how to connect javascript with excel sheet? - làm thế nào để kết nối javascript với trang tính excel?

Các sự kiện

await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let previousSheet = currentSheet.getPrevious();
    previousSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that precedes the active worksheet is "${previousSheet.name}"`);
});
8 và
await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let previousSheet = currentSheet.getPrevious();
    previousSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that precedes the active worksheet is "${previousSheet.name}"`);
});
9 cung cấp cho các cuộc gọi lại của họ với WorksheetColumnSortedEventArgs hoặc WorksheetRowsortedEventArgs. Chúng cung cấp thêm chi tiết về sự kiện này. Cụ thể, cả
await Excel.run(async (context) => {
    let sheets = context.workbook.worksheets;

    let sheet = sheets.add("Sample");
    sheet.load("name, position");

    await context.sync();
    console.log(`Added worksheet named "${sheet.name}" in position ${sheet.position}`);
});
7 đều có thuộc tính
await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let nextSheet = currentSheet.getNext();
    nextSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that follows the active worksheet is "${nextSheet.name}"`);
});
6 đại diện cho các hàng hoặc cột được di chuyển do kết quả của hoạt động sắp xếp. Bất kỳ ô nào có nội dung được sắp xếp được bao gồm, ngay cả khi giá trị của ô đó không phải là một phần của tiêu chí sắp xếp.Quinces" (the values in "4") on the original data, the following highlighted columns are returned by
await Excel.run(async (context) => {
    let myWorkbook = context.workbook;
    let sampleSheet = myWorkbook.worksheets.getActiveWorksheet();
    let copiedSheet = sampleSheet.copy(Excel.WorksheetPositionType.after, sampleSheet);
    await context.sync();
});
1.

Hướng dẫn how to connect javascript with excel sheet? - làm thế nào để kết nối javascript với trang tính excel?

Các hình ảnh sau đây cho thấy các phạm vi được trả về bởi thuộc tính

await Excel.run(async (context) => {
    let currentSheet = context.workbook.worksheets.getActiveWorksheet();
    let nextSheet = currentSheet.getNext();
    nextSheet.load("name");

    await context.sync();
    console.log(`The name of the sheet that follows the active worksheet is "${nextSheet.name}"`);
});
6 cho các sự kiện sắp xếp. Đầu tiên, đây là dữ liệu mẫu trước khi sắp xếp:

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
7

Nếu một loại từ trên xuống dưới được thực hiện trên "Q1" (các giá trị trong "B"), các hàng được tô sáng sau đây được trả về bởi await Excel.run(async (context) => { let myWorkbook = context.workbook; let sampleSheet = myWorkbook.worksheets.getActiveWorksheet(); let copiedSheet = sampleSheet.copy(Excel.WorksheetPositionType.after, sampleSheet); await context.sync(); }); 0.

Nếu một loại từ trái sang phải được thực hiện trên "Quinces" (các giá trị trong "4") trên dữ liệu gốc, các cột được tô sáng sau đây sẽ được trả về bởi

await Excel.run(async (context) => {
    let myWorkbook = context.workbook;
    let sampleSheet = myWorkbook.worksheets.getActiveWorksheet();
    let copiedSheet = sampleSheet.copy(Excel.WorksheetPositionType.after, sampleSheet);
    await context.sync();
});
1.

Mẫu mã sau đây cho thấy cách đăng ký trình xử lý sự kiện cho sự kiện

await Excel.run(async (context) => {
    let myWorkbook = context.workbook;
    let sampleSheet = myWorkbook.worksheets.getActiveWorksheet();
    let copiedSheet = sampleSheet.copy(Excel.WorksheetPositionType.after, sampleSheet);
    await context.sync();
});
2. Cuộc gọi lại của người xử lý xóa màu lấp đầy cho phạm vi, sau đó lấp đầy các ô của các hàng di chuyển.Complete and colors them green. Note that
await Excel.run(async (context) => {
    let myWorkbook = context.workbook;
    let sampleSheet = myWorkbook.worksheets.getActiveWorksheet();
    let copiedSheet = sampleSheet.copy(Excel.WorksheetPositionType.after, sampleSheet);
    await context.sync();
});
4 throws an
await Excel.run(async (context) => {
    let firstSheet = context.workbook.worksheets.getFirst();
    firstSheet.load("name");

    await context.sync();
    console.log(`The name of the first worksheet is "${firstSheet.name}"`);
});
5 error if the specified string doesn't exist in the worksheet. If you're uncertain whether the specified string exists in the worksheet, use the findAllOrNullObject method to gracefully handle that scenario.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
8

Tìm tất cả các ô với văn bản phù hợp

Đối tượng

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    sheet.activate();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
7 có phương thức
await Excel.run(async (context) => {
    let myWorkbook = context.workbook;
    let sampleSheet = myWorkbook.worksheets.getActiveWorksheet();
    let copiedSheet = sampleSheet.copy(Excel.WorksheetPositionType.after, sampleSheet);
    await context.sync();
});
4 để tìm kiếm một chuỗi được chỉ định trong bảng tính. Nó trả về một đối tượng
await Excel.run(async (context) => {
    let myWorkbook = context.workbook;
    let sampleSheet = myWorkbook.worksheets.getActiveWorksheet();
    let copiedSheet = sampleSheet.copy(Excel.WorksheetPositionType.after, sampleSheet);
    await context.sync();
});
5, đây là một tập hợp các đối tượng
await Excel.run(async (context) => {
    let myWorkbook = context.workbook;
    let sampleSheet = myWorkbook.worksheets.getActiveWorksheet();
    let copiedSheet = sampleSheet.copy(Excel.WorksheetPositionType.after, sampleSheet);
    await context.sync();
});
6 có thể được chỉnh sửa cùng một lúc.

  • await Excel.run(async (context) => {
        let sheets = context.workbook.worksheets;
        sheets.load("items/name");
    
        await context.sync();
        if (sheets.items.length === 1) {
            console.log("Unable to delete the only worksheet in the workbook");
        } else {
            let lastSheet = sheets.items[sheets.items.length - 1];
    
            console.log(`Deleting worksheet named "${lastSheet.name}"`);
            lastSheet.delete();
    
            await context.sync();
        }
    });
    
    0: Phạm vi mà bộ lọc được áp dụng, được chỉ định là đối tượng
    await Excel.run(async (context) => {
        let myWorkbook = context.workbook;
        let sampleSheet = myWorkbook.worksheets.getActiveWorksheet();
        let copiedSheet = sampleSheet.copy(Excel.WorksheetPositionType.after, sampleSheet);
        await context.sync();
    });
    
    6 hoặc chuỗi.
  • await Excel.run(async (context) => {
        let sheets = context.workbook.worksheets;
        sheets.load("items/name");
    
        await context.sync();
        if (sheets.items.length === 1) {
            console.log("Unable to delete the only worksheet in the workbook");
        } else {
            let lastSheet = sheets.items[sheets.items.length - 1];
    
            console.log(`Deleting worksheet named "${lastSheet.name}"`);
            lastSheet.delete();
    
            await context.sync();
        }
    });
    
    2: Chỉ số cột dựa trên không theo tiêu chí bộ lọc được đánh giá.
  • await Excel.run(async (context) => {
        let sheets = context.workbook.worksheets;
        sheets.load("items/name");
    
        await context.sync();
        if (sheets.items.length === 1) {
            console.log("Unable to delete the only worksheet in the workbook");
        } else {
            let lastSheet = sheets.items[sheets.items.length - 1];
    
            console.log(`Deleting worksheet named "${lastSheet.name}"`);
            lastSheet.delete();
    
            await context.sync();
        }
    });
    
    3: Một đối tượng FilterCriteria Xác định hàng nào nên được lọc dựa trên ô của cột.

Mẫu mã đầu tiên cho thấy cách thêm bộ lọc vào phạm vi đã sử dụng của bảng tính. Bộ lọc này sẽ ẩn các mục không nằm trong top 25%, dựa trên các giá trị trong cột 3.3.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
9

Mẫu mã tiếp theo cho thấy cách làm mới bộ lọc tự động bằng phương pháp

await Excel.run(async (context) => {
    let sheets = context.workbook.worksheets;
    sheets.load("items/name");

    await context.sync();
    if (sheets.items.length === 1) {
        console.log("Unable to delete the only worksheet in the workbook");
    } else {
        let lastSheet = sheets.items[sheets.items.length - 1];

        console.log(`Deleting worksheet named "${lastSheet.name}"`);
        lastSheet.delete();

        await context.sync();
    }
});
4. Điều này nên được thực hiện khi dữ liệu trong phạm vi thay đổi.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    sheet.activate();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
0

Mẫu mã sau đây cho thấy cách sử dụng phương thức

await Excel.run(async (context) => {
    let sheets = context.workbook.worksheets;
    sheets.load("items/name");

    await context.sync();
    if (sheets.items.length === 1) {
        console.log("Unable to delete the only worksheet in the workbook");
    } else {
        let lastSheet = sheets.items[sheets.items.length - 1];

        console.log(`Deleting worksheet named "${lastSheet.name}"`);
        lastSheet.delete();

        await context.sync();
    }
});
5 để xóa bộ lọc tự động khỏi chỉ một cột, trong khi để bộ lọc hoạt động trên các cột khác.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    sheet.activate();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
1

Mẫu mã tự động cuối cùng cho thấy cách loại bỏ bộ lọc tự động khỏi bảng tính bằng phương pháp

await Excel.run(async (context) => {
    let sheets = context.workbook.worksheets;
    sheets.load("items/name");

    await context.sync();
    if (sheets.items.length === 1) {
        console.log("Unable to delete the only worksheet in the workbook");
    } else {
        let lastSheet = sheets.items[sheets.items.length - 1];

        console.log(`Deleting worksheet named "${lastSheet.name}"`);
        lastSheet.delete();

        await context.sync();
    }
});
6.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    sheet.activate();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
2

Một

await Excel.run(async (context) => {
    let sheets = context.workbook.worksheets;
    sheets.load("items/name");

    await context.sync();
    if (sheets.items.length === 1) {
        console.log("Unable to delete the only worksheet in the workbook");
    } else {
        let lastSheet = sheets.items[sheets.items.length - 1];

        console.log(`Deleting worksheet named "${lastSheet.name}"`);
        lastSheet.delete();

        await context.sync();
    }
});
7 cũng có thể được áp dụng cho các bảng riêng lẻ. Xem công việc với các bảng bằng API Excel JavaScript để biết thêm thông tin.

Bảo vệ dữ liệu

Bổ trợ của bạn có thể kiểm soát khả năng chỉnh sửa dữ liệu của người dùng trong bảng tính. Thuộc tính

await Excel.run(async (context) => {
    let sheets = context.workbook.worksheets;
    sheets.load("items/name");

    await context.sync();
    if (sheets.items.length === 1) {
        console.log("Unable to delete the only worksheet in the workbook");
    } else {
        let lastSheet = sheets.items[sheets.items.length - 1];

        console.log(`Deleting worksheet named "${lastSheet.name}"`);
        lastSheet.delete();

        await context.sync();
    }
});
8 của bảng tính là một đối tượng bảng tính với phương thức
await Excel.run(async (context) => {
    let sheets = context.workbook.worksheets;
    sheets.load("items/name");

    await context.sync();
    if (sheets.items.length === 1) {
        console.log("Unable to delete the only worksheet in the workbook");
    } else {
        let lastSheet = sheets.items[sheets.items.length - 1];

        console.log(`Deleting worksheet named "${lastSheet.name}"`);
        lastSheet.delete();

        await context.sync();
    }
});
9. Ví dụ sau đây cho thấy một kịch bản cơ bản chuyển đổi sự bảo vệ hoàn toàn của bảng tính hoạt động.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    sheet.activate();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
3

Phương thức

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
00 có hai tham số tùy chọn:

  • await Excel.run(async (context) => {
        let sheet = context.workbook.worksheets.getActiveWorksheet();
        sheet.load("name");
    
        await context.sync();
        console.log(`The active worksheet is "${sheet.name}"`);
    });
    
    01: Một bảng tính đối tượng Xác định các hạn chế chỉnh sửa cụ thể.
  • await Excel.run(async (context) => {
        let sheet = context.workbook.worksheets.getActiveWorksheet();
        sheet.load("name");
    
        await context.sync();
        console.log(`The active worksheet is "${sheet.name}"`);
    });
    
    02: Một chuỗi đại diện cho mật khẩu cần thiết cho người dùng để bỏ qua bảo vệ và chỉnh sửa bảng tính.

Bài viết bảo vệ một bảng tính có thêm thông tin về bảo vệ bảng tính và cách thay đổi nó thông qua UI Excel.

Phát hiện các thay đổi đối với trạng thái bảo vệ bảng tính

Trạng thái bảo vệ của một bảng tính có thể được thay đổi bởi một bổ trợ hoặc thông qua UI Excel. Để phát hiện các thay đổi đối với trạng thái bảo vệ, hãy đăng ký một trình xử lý sự kiện cho sự kiện ____103 của bảng tính. Trình xử lý sự kiện cho sự kiện

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
03 nhận được đối tượng
await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
05 khi sự kiện bị bắn.

Mẫu mã sau đây cho thấy cách đăng ký trình xử lý sự kiện

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
03 và sử dụng đối tượng
await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
05 để truy xuất các thuộc tính
await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
08,
await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
09 và
await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
10 của sự kiện.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    sheet.activate();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
4

Bố cục trang và cài đặt in

Bổ trợ có quyền truy cập vào cài đặt bố cục trang ở cấp bảng tính. Những điều khiển này làm thế nào trang tính được in. Một đối tượng

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    sheet.activate();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
7 có ba thuộc tính liên quan đến bố cục:
await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
12,
await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
13,
await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
14.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
15 và
await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
16 là PageBreakCollections. Đây là các bộ sưu tập của các trang phá vỡ, trong đó chỉ định các phạm vi mà các lần ngắt trang thủ công được chèn. Mẫu mã sau đây thêm một lần ngắt trang ngang trên hàng 21.21.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    sheet.activate();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
5

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getActiveWorksheet();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
17 là một đối tượng Pagelayout. Đối tượng này chứa các cài đặt bố cục và in không phụ thuộc vào bất kỳ triển khai cụ thể của máy in. Các cài đặt này bao gồm lề, định hướng, đánh số trang, hàng tiêu đề và khu vực in.

Mẫu mã sau tập trung vào trang (cả theo chiều dọc và chiều ngang), đặt một hàng tiêu đề sẽ được in ở đầu mỗi trang và đặt khu vực in thành phần phụ của bảng tính.

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    sheet.activate();
    sheet.load("name");

    await context.sync();
    console.log(`The active worksheet is "${sheet.name}"`);
});
6

Xem thêm

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

Nhận xét

Gửi và xem phản hồi cho

Làm cách nào để kết nối JavaScript với Excel?

Cách sử dụng JavaScript với Excel..
Tạo khối JavaScript của bạn. Khối này cho biết trình duyệt web rằng mã chứa có thể thực thi các tập lệnh. ....
Bắt đầu biến ứng dụng Excel. ....
Tạo bảng tính và kích hoạt nó. ....
Viết một số văn bản vào bảng tính Excel. ....
Lưu tệp Excel ..

JavaScript có thể tương tác với Excel không?

Một bổ trợ Excel tương tác với các đối tượng trong Excel bằng cách sử dụng API JavaScript Office, bao gồm hai mô hình đối tượng JavaScript: Excel JavaScript API: Được giới thiệu với Office 2016, API JavaScript Excel cung cấp các đối tượng được gõ mạnh mà bạn có thể sử dụng để truy cập bảng tính,Phạm vi, bảng, biểu đồ, và nhiều hơn nữa., which includes two JavaScript object models: Excel JavaScript API: Introduced with Office 2016, the Excel JavaScript API provides strongly-typed objects that you can use to access worksheets, ranges, tables, charts, and more.

Làm thế nào tôi có thể sử dụng JavaScript trong Excel Online?

Có hai cách để làm điều này: trực tiếp sử dụng API trực tuyến Excel ...
Cài đặt gói JavaScript với NPM ..
Chạy yêu cầu API trực tuyến Excel với JavaScript ..
Tạo và sử dụng API tờ2API của riêng bạn ..

Làm cách nào để mở một tệp excel trong javascript?

Điều này có thể được thực hiện khá dễ dàng bằng cách sử dụng SheetJS.Nhập {Đọc, WriteFileXLSX, Utils} từ "https://cdn.sheetjs.com/xlsx-0.18.7/package/xlsx.mjs";const workbook = read (data, {type: 'nhị phân',});Dữ liệu là chuỗi nhị phân do đọc tệp Excel dưới dạng chuỗi nhị phân với API Filereader.using SheetJS. import { read, writeFileXLSX, utils } from "https://cdn.sheetjs.com/xlsx-0.18.7/package/xlsx.mjs"; const workbook = read(data, { type:'binary', }); data is the binary string resulting from reading an Excel file as a binary string with the FileReader API.