Bỏ qua nội dung chính
Trình duyệt này không còn được hỗ trợ.
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.
Tổng quan về API JavaScript Excel
- Bài báo
- 25/03/2022
- 2 phút để đọc
Trong bài viết này
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:
API JavaScript Excel: Đây là các API dành riêng cho ứng dụng cho Excel. Đượ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.v.: These are the application-specific APIs for Excel. Introduced with Office 2016, the Excel JavaScript API provides strongly-typed objects that you can use to access worksheets, ranges, tables, charts, and more.
API thông thường: Được giới thiệu với Office 2013, API phổ biến có thể được sử dụng để truy cập các tính năng như UI, hộp thoại và cài đặt máy khách phổ biến trên nhiều loại ứng dụng Office.: Introduced with Office 2013, the Common API can be used to access features such as UI, dialogs, and client settings that are common across multiple types of Office applications.
Phần này của tài liệu tập trung vào API JavaScript Excel, mà bạn sẽ sử dụng để phát triển phần lớn chức năng trong các phần bổ sung mà mục tiêu Excel trên web hoặc Excel 2016 trở lên. Để biết thông tin về API phổ biến, hãy xem mô hình đối tượng API JavaScript thông thường.
Tìm hiểu các khái niệm mô hình đối tượng
Xem mô hình đối tượng Excel JavaScript trong các tiện ích bổ sung văn phòng để biết thông tin về các khái niệm mô hình đối tượng quan trọng.
Để có trải nghiệm thực hành bằng cách sử dụng API JavaScript Excel để truy cập các đối tượng trong Excel, hãy hoàn thành hướng dẫn bổ trợ Excel.
Tìm hiểu khả năng API
Mỗi tính năng API Excel chính có một bài viết hoặc bộ bài viết khám phá những gì tính năng đó có thể làm và mô hình đối tượng có liên quan.
- Biểu đồ
- Bình luận
- Định dạng có điều kiện
- Chức năng tùy chỉnh
- Xác nhận dữ liệu
- Loại dữ liệu
- Sự kiện
- Pivottables
- Phạm vi và tế bào
- RangeAs [nhiều phạm vi]
- Hình dạng
- Những cái bàn
- Sách bài tập và API cấp ứng dụng
- Bảng tính
Để biết thông tin chi tiết về mô hình đối tượng API JavaScript Excel, hãy xem Tài liệu tham khảo API JavaScript Excel.
Thử các mẫu mã trong phòng thí nghiệm tập lệnh
Sử dụng Script Lab để bắt đầu nhanh chóng với một bộ sưu tập các mẫu tích hợp cho thấy cách hoàn thành các tác vụ với API. Bạn có thể chạy các mẫu trong phòng thí nghiệm tập lệnh để thấy kết quả ngay lập tức trong ngăn tác vụ hoặc bảng tính, kiểm tra các mẫu để tìm hiểu cách thức hoạt động của API và thậm chí sử dụng các mẫu để tạo mẫu bổ trợ của riêng bạn.
Xem thêm
- Tài liệu bổ trợ Excel
- Tổng quan bổ sung Excel
- Tham chiếu API JavaScript Excel
- Ứng dụng khách hàng văn phòng và tính khả dụng của nền tảng cho các tiện ích bổ sung văn phòng
- Sử dụng mô hình API dành riêng cho ứng dụng
Nhận xét
Gửi và xem phản hồi cho
Bỏ qua nội dung chính
Trình duyệt này không còn được hỗ trợ.
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 đó.
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}"`];
}];
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ậ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.
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}"`];
}];
Đặ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.
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}"`];
}];
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}"`];
}];
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}"`];
}];
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.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ận bảng tính cuối cùng
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.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}"`];
}];
Nhận bảng tính tiếp theo
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.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}"`];
}];
Nhận bảng tính trước đó
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.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}"`];
}];
Thêm một bảng tính
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ó.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}`];
}];
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: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 can take two parameters, both of which are optional:
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ếuawait 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 lastSheet = context.workbook.worksheets.getLast[]; lastSheet.load["name"]; await context.sync[]; console.log[`The name of the last worksheet is "${lastSheet.name}"`]; }];
1 hoặcawait 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 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 sao chép bảng tính hiện tại và chèn bảng mới trực tiếp sau bảng tính hiện tạ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[];
}];
Xóa một bảng tính
Mẫu mã sau đây xóa bảng tính cuối cùng trong sổ làm việc [miễn là nó không phải là bảng duy nhất trong sổ làm việc] 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["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ú
Một bảng tính với khả năng hiển thị của "rất ẩn" không thể bị xóa bằng phương pháp
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}"`];
}];
3. Nếu bạn muốn xóa bảng tính, trước tiên bạn phải thay đổi khả năng hiển thị.Đổi tên một bảng tính
Mẫu mã sau đây thay đổi tên của bảng tính hoạt động thành tên mới.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}"`];
}];
0Di chuyển một bảng tính
Mẫu mã sau đây di chuyển một bảng tính từ vị trí cuối cùng trong sổ làm việc sang vị trí đầu tiên trong sổ làm việ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Đặt khả năng hiển thị bảng tính
Những ví dụ này cho thấy cách đặt tầm nhìn của một bảng tính.
Ẩn một bảng tính
Mẫu mã sau đây đặt khả năng hiển thị của bảng tính được đặt tên là ẩn, 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.Sample 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}"`];
}];
2Xé một bảng tính
Mẫu mã sau đây đặt khả năng hiển thị của bảng tính được đặt tên là hiển thị, 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.Sample 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}"`];
}];
3Nhậ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}"`];
}];
4Phá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}"`];
}];
5Phá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}"`];
}];
6Xử 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.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.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.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}"`];
}];
7Nế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.
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[];
}];
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}"`];
}];
8Tì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 tất cả cùng một lúc.- Mẫu mã sau đây tìm thấy tất cả các ô có giá trị bằng chuỗi hoàn chỉnh và màu chúng xanh. Lưu ý rằng
4 đã ném lỗiawait 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 nếu chuỗi được chỉ định không tồn tại trong bảng tính. Nếu bạn không chắc chắn liệu chuỗi được chỉ định có tồn tại trong bảng tính hay không, hãy sử dụng phương thức FindallArnullObject để xử lý kịch bản đó một cách duyên dáng.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}"`]; }];
- Dữ liệu lọc
- Một bộ lọc tự động áp dụng các bộ lọc dữ liệu trên một phạm vi trong bảng tính. Điều này được tạo bằng
9, có các tham số sau.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 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.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}"`];
}];
9await 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 sheet = context.workbook.worksheets.getItem["Sample"];
sheet.activate[];
sheet.load["name"];
await context.sync[];
console.log[`The active worksheet is "${sheet.name}"`];
}];
0await 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.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}"`];
}];
1Mẫ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.
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}"`];
}];
2Mẫ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.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 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[];
}
}];
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}"`];
}];
3Mộ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
8 của bảng tính là một đối tượng bảng tính với phương thứcawait 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 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[]; } }];
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}"`];
}];
4Bố 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}"`];
}];
5await 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}"`];
}];
6Xem 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