- หน้าแรกสำหรับนักพัฒนา
- ฝังเอดิเตอร์
- Automation API
Automation API
Automation API
ควบคุมเอกสารที่เปิดอยู่จากหน้าของคุณ: อ่านและเขียนไฟล์ Word, Excel, PowerPoint และ PDF ไฟล์โค้ด และเอกสารลายมือ ในขณะที่ผู้ใช้ทำงานอยู่ ทุกการเรียกใช้ promise มี type (mroffice-embed มีไฟล์ .d.ts มาให้) และถูกตรวจสอบในเอดิเตอร์เทียบกับเซสชัน: เซสชันแบบดูอย่างเดียวอ่านได้แต่เขียนไม่ได้เลย การเปลี่ยนแปลงผ่านคำสั่งของเอดิเตอร์เอง ซึ่งเป็นชุดเดียวกับที่ริบบอนเรียกใช้ จึงนับเป็นการแก้ไขอีกหนึ่งครั้งของผู้ใช้ในเซสชัน: เลิกทำได้ ทุกคนในเอกสารเห็นทันที การติดตามการเปลี่ยนแปลงจะบันทึกไว้ และถูกบันทึกไปพร้อมกับไฟล์
const editor = MROffice.open({ container: "#editor", appId: APP_ID, token: fetchToken });
await editor.ready;
const caps = await editor.getCapabilities(); // { apiVersion: 1, kind: "docx", mode: "edit", methods: [...], exportFormats }
// Word
await editor.word.insertText("CONFIDENTIAL", { location: "start" });
const { count } = await editor.word.replaceText("ACME", "Acme Ltd.", { matchCase: true });
await editor.word.insertMarkdown("## Terms\n\n- Net 30\n- Late fee 2%", { location: { bookmark: "terms" } });
await editor.word.insertTable([["Item", "Price"], ["Pen", 2]], { header: true, location: "end" });
await editor.word.addComment("Is 30 days right?", { anchorText: "within 30 days" });
const blocks = await editor.word.getContent(); // [{ type: "paragraph", id, style, text } | { type: "table", id, rows }]
// ไฟล์ในรูปแบบ Blob
const { blob, name } = await editor.document.export("pdf");
await editor.document.save(); // เหมือน requestSave()
try {
await editor.word.insertText("x");
} catch (error) {
// error.code: "embed_view_only", "embed_target_not_found", … ; error.hint, error.docs, error.details
}เมธอด
| เมธอด | ทำอะไร |
|---|---|
getCapabilities() | apiVersion ประเภทของไฟล์ โหมดของเซสชัน เมธอดที่เรียกได้ในขณะนี้ และรูปแบบที่ document.export เขียนได้ |
document.getInfo() | key, name, kind, mode, version, partnerVersion และ dirty (มีการแก้ไขที่กำลังส่งไปยัง MR Office) |
document.export(format?) | เอกสารในรูปแบบไฟล์ { blob, name, mimeType, size }: docx, pdf, odt, rtf, html, md หรือ txt สำหรับ Word; xlsx, pdf, csv, ods หรือ html สำหรับ Excel; pptx, pdf หรือ odp สำหรับ PowerPoint; pdf สำหรับ PDF สิ่งที่กำลังพิมพ์จะถูกเก็บก่อน และจะถูกปฏิเสธเมื่อ ui.features.export เป็น false |
document.save() | บันทึกลงไฟล์ของคุณทันที เหมือน requestSave() และ resolve ด้วยไฟล์ที่บันทึกแล้ว |
word.getText(), word.getContent() | ข้อความธรรมดา และเนื้อหาในรูปแบบย่อหน้า ({ type, id, style, level?, text }) กับตาราง ({ type, id, rows }) พร้อม id ของบล็อกที่การเรียกอื่นใช้ |
word.getSelection(), word.setSelection(target) | สิ่งที่เลือกอยู่ ({ text, isEmpty, blockId }) และเลือกรายการที่ตรงกันลำดับที่ occurrence ของข้อความ บุ๊กมาร์ก ตัวควบคุมเนื้อหา บล็อก หรือ { all: true } |
word.insertText / insertHtml / insertMarkdown(content, { location? }) | ที่ส่วนที่เลือก (แทนที่ส่วนนั้น) ที่ "start" หรือ "end" หรือที่ { bookmark }, { contentControl: { tag | title } } หรือ { blockId } โดย position เป็น before, after หรือ replace HTML และ Markdown ถูกอ่านเหมือนที่การวางของเอดิเตอร์เองอ่าน สิ่งที่ไม่มีที่รองรับ (สคริปต์ สไตล์ ตัวจัดการเหตุการณ์) จะถูกตัดทิ้ง ตัวควบคุมเนื้อหาจะคงตัวตนเดิมเมื่อข้อความถูกเติม |
word.replaceText(find, replace, { matchCase?, wholeWord? }) | แทนที่ทั้งหมดของ Word: ทุกตำแหน่งที่พบ โดยคงการจัดรูปแบบ และถูกติดตามเมื่อเปิดการติดตามการเปลี่ยนแปลงอยู่ และ resolve ด้วย { count } |
word.insertTable(rows, { header?, location? }) | ตารางจากแถวของค่า (แถวแรกเป็นแถวหัวตารางเมื่อ header: true) |
word.addComment(text, { anchorText?, occurrence? }) | ความคิดเห็นบนส่วนที่เลือก หรือบนรายการที่ตรงกันลำดับที่ occurrence ของ anchorText โดยผู้ใช้ของเซสชัน |
word.getTrackedChanges(), acceptTrackedChanges(index?), rejectTrackedChanges(index?), setTrackChanges(on) | รีวิว > ติดตามการเปลี่ยนแปลง: การเปลี่ยนแปลง (index, kind, author, date, text) ยอมรับหรือปฏิเสธหนึ่งรายการตามดัชนี หรือทั้งหมด และเปิดหรือปิดการติดตามสำหรับทุกคน |
word.getProperties(), word.setProperties(properties) | ไฟล์ > ข้อมูล > คุณสมบัติ: title, subject, author, company, category, keywords, abstract, publishDate, companyAddress, companyPhone, companyFax, companyEmail โดยบันทึกใน docProps ของไฟล์ |
word.getBookmarks(), word.getContentControls() | บุ๊กมาร์ก และตัวควบคุมเนื้อหาพร้อม tag, title, type, text และสถานะว่าถูกล็อกหรือไม่ |
excel.getSheets(), excel.addSheet(name?), excel.getUsedRange(sheet?) | แผ่นงาน ({ id, name, index, active, hidden }) แผ่นงานใหม่ต่อจากแผ่นสุดท้าย และช่วงที่ใช้งานของแผ่นงาน (ค่าเริ่มต้นคือแผ่นที่เปิดอยู่) |
excel.getRange(address), setValues(address, values, { asTyped? }), setFormulas(address, formulas) | ที่อยู่แบบ A1 ("B2:D9", "Sales!A1:C3", "'My sheet'!A:A" และใช้แผ่นงานที่เปิดอยู่เมื่อไม่ได้ระบุ) getRange ให้ values (ค่าที่คำนวณแล้ว) และ formulas (ตามที่พิมพ์) ทีละแถว การเขียนเทียบเท่าการป้อนค่าของ Excel: "=…" เป็นสูตร ส่วน "50%", "$1,000" หรือ "2026-10-01" ถูกอ่านเหมือนที่ Excel อ่านเมื่อพิมพ์ (asTyped: false เก็บข้อความตามที่ให้มา) และ resolve ด้วยช่วงตามที่เขียน |
powerpoint.getSlides(), addSlide({ title?, body?, layout?, index? }), replaceText(find, replace), goToSlide(index) | สไลด์ ({ index, id, title, text, hidden }) สไลด์ใหม่ที่เติมชื่อเรื่องและเนื้อหาของเค้าโครงแล้ว (ค่าเริ่มต้น title-content) แทนที่ทั้งหมดทั่วทั้งงานนำเสนอ และแสดงสไลด์ |
pdf.search(text, { limit? }), pdf.goToPage(page) | การค้นหาของแถบค้นหาเอง ({ page, snippet, index } ต่อรายการที่พบ) และแสดงหน้า |
pdf.addHighlight(text, { page?, occurrence?, style? }), pdf.addComment(text, { page? }) | คำสั่งไฮไลต์ของริบบอน (หรือขีดเส้นใต้ ขีดทับ เส้นหยัก) บนรายการที่ตรงกัน บันทึกเป็นมาร์กอัปของ PDF เอง และโน้ตแบบแปะบนหน้า (ค่าเริ่มต้นคือหน้าที่แสดงอยู่) |
code.getText(), code.getSelection(), code.setSelection(from, to?) | ข้อความของไฟล์โค้ดตามที่เอดิเตอร์เก็บไว้ ({ text, language, lineCount } ขึ้นบรรทัดใหม่ด้วย "\n" และอักขระขึ้นบรรทัดใหม่เดิมของไฟล์จะกลับมาเมื่อบันทึก) ส่วนที่เลือก ({ text, isEmpty, from, to } แต่ละตัวเป็น { line, column, offset } เริ่มนับจาก 1) และเลือกจาก { line, column? } หนึ่งไปยังอีกตำแหน่ง |
code.setText(text), code.insertText(text, { location? }) | แทนที่ทั้งไฟล์ หรือแทรกที่ส่วนที่เลือก (แทนที่ส่วนนั้น) ที่ "start" หรือ "end" หรือที่ { line, column? } โดยเขียนเหมือนผู้ใช้พิมพ์: เลิกทำได้ |
ink.getStrokes(), ink.clear() | เส้นขีดของปากกา ดินสอ และปากกาเน้นข้อความในเอกสารลายมือ ({ count, strokes: [{ index, tool, color, widthPt }] }) และลบทั้งหมดในขั้นตอนเดียว (เลิกทำแล้วจะกลับมา) |
Excel, PowerPoint และ PDF
// Excel (.xlsx)
const sheets = await editor.excel.getSheets(); // [{ id, name, index, active, hidden }]
await editor.excel.setValues("Sales!A1:B3", [["Region", "Q1"], ["North", 10], ["South", 30]]);
await editor.excel.setFormulas("Sales!B4", [["=SUM(B2:B3)"]]);
const range = await editor.excel.getRange("Sales!A1:B4"); // { address, values: [[…]], formulas: [[…]] }
await editor.excel.addSheet("Summary");
const used = await editor.excel.getUsedRange("Sales"); // { address: "Sales!A1:B4", rowCount, columnCount }
// PowerPoint (.pptx)
await editor.powerpoint.addSlide({ title: "Next steps", body: "Sign\nShip", index: 1 });
await editor.powerpoint.replaceText("2025", "2026");
const { slides } = await editor.powerpoint.getSlides(); // [{ index, id, title, text, hidden }]
await editor.powerpoint.goToSlide(0);
// PDF
const hits = await editor.pdf.search("indemnify"); // [{ page, snippet, index }]
await editor.pdf.addHighlight("indemnify", { page: hits[0].page });
await editor.pdf.addComment("Check this clause", { page: 3 });
await editor.pdf.goToPage(1);ตำแหน่งและกฎ
- ให้เรียกหลัง ready การเรียกที่ทำก่อนหน้านั้นจะรอ การเรียกทำงานทีละรายการตามลำดับที่คุณเรียก
- ตำแหน่งจะไม่ข้ามขอบเขตของเฟรม: ระบุตำแหน่งด้วยข้อความ id ของบล็อก (จาก word.getContent) บุ๊กมาร์ก tag หรือ title ของตัวควบคุมเนื้อหา ที่อยู่แบบ A1 ดัชนีสไลด์ หรือหมายเลขหน้า
- เมธอดที่เขียนทำงานได้เฉพาะในเซสชัน edit: ในเซสชัน view จะถูก reject ด้วย embed_view_only และในเซสชัน comment, review หรือ fillForms ด้วย embed_mode_read_only ส่วน word.addComment และ pdf.addComment ทำงานได้ในเซสชัน comment หรือ review ที่แสดงความคิดเห็นได้ด้วย (มิฉะนั้นจะได้ embed_comment_not_allowed) การยอมรับหรือปฏิเสธการเปลี่ยนแปลงต้องมี permissions.acceptRejectChanges และ document.export ต้องมี permissions.download (embed_download_not_allowed) กับรูปแบบที่ permissions.export ระบุไว้ (embed_export_format_not_allowed) เมธอดสำหรับไฟล์ประเภทอื่นจะถูก reject ด้วย embed_command_unsupported และเมธอดหรือ apiVersion ที่ไม่รู้จักด้วย embed_command_unknown โดยไม่มีสิ่งใดไปถึงเอกสาร
- ข้อผิดพลาดอื่น: embed_command_invalid (error.details.fields ระบุพารามิเตอร์), embed_target_not_found, embed_command_not_applicable (error.details บอกเหตุผล), embed_read_only (ไฟล์ที่ป้องกันหรือล็อกไว้ หรือไฟล์ที่ยังเตรียมไม่เสร็จ), embed_editor_not_ready, embed_export_disabled, embed_command_failed แต่ละรายการมี error.hint และ error.docs (ดูข้อผิดพลาดและการดีบัก)
- ขีดจำกัดต่อการเรียก: ข้อความ HTML หรือ Markdown ขนาด 1 MB เซลล์ 100,000 เซลล์ต่อช่วง และตารางสูงสุด 500 แถว แถวละ 63 เซลล์
- นี่คือ apiVersion 1 ภายในเวอร์ชันเดียวกัน เมธอดจะถูกเพิ่มเท่านั้น ไม่ถูกเปลี่ยนแปลง: getCapabilities() บอกว่าเอดิเตอร์นี้ใช้เวอร์ชันใด และหน้าที่เขียนสำหรับเอดิเตอร์รุ่นใหม่กว่าสามารถตรวจว่ามีเมธอดก่อนเรียกได้
เมื่อไม่ใช้ embed.js
หากคุณสร้างโฮสต์เฟรมของคุณเอง การเรียกหนึ่งครั้งคือข้อความ command หนึ่งข้อความ และคำตอบคือ commandResult ที่มี id เดียวกัน (เอกสารอ้างอิง postMessage) เฟรมจะอ่านคำสั่งจาก origin ที่ส่ง init มาเท่านั้น
// โฮสต์เฟรมของคุณเอง: หนึ่ง "command" ต่อการเรียก ตอบกลับด้วย "commandResult" ที่มี id เดียวกัน
frame.postMessage({ source: "mroffice-embed", version: 1, type: "command", id: "c1",
payload: { method: "word.replaceText", params: { find: "ACME", replace: "Acme" }, apiVersion: 1 } }, EDITOR_ORIGIN);
// ← { source: "mroffice-embed", version: 1, type: "commandResult", id: "c1", payload: { ok: true, result: { count: 3 } } }
// ← { …, payload: { ok: false, error: { code: "embed_view_only", message: "…" } } }เทียบกับ Office.js และ ONLYOFFICE
ชื่อต่าง ๆ เป็นไปตามโฮสต์ของ Office.js: เนื้อหา ย่อหน้า ตาราง และตัวควบคุมเนื้อหาของ Word ค่าและสูตรของ Range ใน Excel และสไลด์ของ PowerPoint ชุดเมธอดครอบคลุมสิ่งที่ connector ของ Automation API ใน ONLYOFFICE มีให้สำหรับงานเดียวกัน (executeMethod PasteHtml, SearchAndReplace, AddComment, AcceptReviewChanges, GetAllContentControls) ในรูปแบบ promise ที่ reject พร้อมรหัสข้อผิดพลาดแทน callback และต่างจากมาโครที่ทำงานโดยลำพัง ตรงที่แต่ละการเรียกเป็นการแก้ไขของผู้ใช้ที่อยู่ในเอดิเตอร์ ภายใต้สิทธิ์ของเซสชันของผู้นั้น