-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Word] Add snippets for APIs re pages, panes, windows #974
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,244 @@ | ||||||||||||
order: 4 | ||||||||||||
id: word-ranges-get-pages | ||||||||||||
name: 'Work with pages, panes, and windows' | ||||||||||||
description: 'Shows how to work with pages, panes, and windows APIs.' | ||||||||||||
author: yilin4 | ||||||||||||
host: WORD | ||||||||||||
api_set: | ||||||||||||
WordApiDesktop: '1.2' | ||||||||||||
script: | ||||||||||||
content: | | ||||||||||||
$("#get-pages-selected-range").on("click", () => tryCatch(getPagesOfSelectedRange)); | ||||||||||||
$("#get-pages-third-paragraph").on("click", () => tryCatch(getPagesOfThirdParagraph)); | ||||||||||||
$("#get-pages-enclosing-viewport").on("click", () => tryCatch(getPagesEnclosingViewport)); | ||||||||||||
$("#get-all-pages").on("click", () => tryCatch(getAllPages)); | ||||||||||||
$("#setup").on("click", () => tryCatch(setup)); | ||||||||||||
|
||||||||||||
async function getPagesOfSelectedRange() { | ||||||||||||
await Word.run(async (context) => { | ||||||||||||
// Gets pages of the selection. | ||||||||||||
const pages: Word.PageCollection = context.document.getSelection().pages; | ||||||||||||
pages.load(); | ||||||||||||
await context.sync(); | ||||||||||||
|
||||||||||||
console.log(pages); | ||||||||||||
// Log info for pages included in selection. | ||||||||||||
for (let i = 0; i < pages.items.length; i++) { | ||||||||||||
let page = pages.items[i]; | ||||||||||||
page.load(); | ||||||||||||
await context.sync(); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should avoid doing load/sync in a loop. It's a bad pattern. This article has some tips to avoid that: https://learn.microsoft.com/en-us/office/dev/add-ins/concepts/correlated-objects-pattern |
||||||||||||
|
||||||||||||
console.log(`Index info for page ${i + 1} in the selection: ${page.index}`); | ||||||||||||
const range = page.getRange(); | ||||||||||||
range.load(); | ||||||||||||
await context.sync(); | ||||||||||||
|
||||||||||||
console.log("Text of that page in the selection:", range.text); | ||||||||||||
} | ||||||||||||
}); | ||||||||||||
} | ||||||||||||
|
||||||||||||
async function getPagesOfThirdParagraph() { | ||||||||||||
await Word.run(async (context) => { | ||||||||||||
// Gets the pages that the third paragraph is found on. | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The dangling "on" seemed off, but I think this is the most concise way to articulate the relationships.
Suggested change
|
||||||||||||
const paragraphs: Word.ParagraphCollection = context.document.body.paragraphs; | ||||||||||||
paragraphs.load(); | ||||||||||||
await context.sync(); | ||||||||||||
|
||||||||||||
const paraThree = paragraphs.items[2]; | ||||||||||||
const rangeOfParagraph = paraThree.getRange(); | ||||||||||||
await context.sync(); | ||||||||||||
|
||||||||||||
const pages: Word.PageCollection = rangeOfParagraph.pages; | ||||||||||||
pages.load(); | ||||||||||||
await context.sync(); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need three roundtrips to the document before even getting to the API in question? If so, that seems like a product flaw. |
||||||||||||
|
||||||||||||
console.log(pages); | ||||||||||||
// Log into for pages in range. | ||||||||||||
for (let i = 0; i < pages.items.length; i++) { | ||||||||||||
let page = pages.items[i]; | ||||||||||||
page.load(); | ||||||||||||
await context.sync(); | ||||||||||||
|
||||||||||||
console.log(`Index of page ${i + 1} that the third paragraph is found on: ${page.index}`); | ||||||||||||
const range = page.getRange(); | ||||||||||||
range.load(); | ||||||||||||
await context.sync(); | ||||||||||||
|
||||||||||||
console.log("Text of that page:", range.text); | ||||||||||||
} | ||||||||||||
}); | ||||||||||||
} | ||||||||||||
|
||||||||||||
async function getPagesEnclosingViewport() { | ||||||||||||
await Word.run(async (context) => { | ||||||||||||
// Gets the pages enclosing the viewport. | ||||||||||||
|
||||||||||||
// Get the active window. | ||||||||||||
let activeWindow = context.document.activeWindow; | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We switched from let to const partway through the snippet. Let's use const here and elsewhere.
Suggested change
|
||||||||||||
activeWindow.load(); | ||||||||||||
await context.sync(); | ||||||||||||
|
||||||||||||
// Get the active pane. | ||||||||||||
let activePane = activeWindow.activePane; | ||||||||||||
activePane.load(); | ||||||||||||
await context.sync(); | ||||||||||||
// Get pages enclosing the viewport. | ||||||||||||
Comment on lines
+85
to
+86
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find adding a blank line after syncs helps with readability.
Suggested change
|
||||||||||||
let pages: Word.PageCollection = activePane.pagesEnclosingViewport; | ||||||||||||
pages.load(); | ||||||||||||
await context.sync(); | ||||||||||||
|
||||||||||||
// Log the number of pages. | ||||||||||||
let pageCount = pages.items.length; | ||||||||||||
console.log(`Number of pages enclosing the viewport: ${pageCount}`); | ||||||||||||
// Log index info of these pages. | ||||||||||||
for (let i = 0; i < pages.items.length; i++) { | ||||||||||||
let page = pages.items[i]; | ||||||||||||
page.load(); | ||||||||||||
await context.sync(); | ||||||||||||
|
||||||||||||
console.log(`Page index: ${page.index}`); | ||||||||||||
} | ||||||||||||
await context.sync(); | ||||||||||||
}); | ||||||||||||
} | ||||||||||||
|
||||||||||||
async function getAllPages() { | ||||||||||||
await Word.run(async (context) => { | ||||||||||||
// Gets the first paragraph of each page. | ||||||||||||
console.log("Getting first paragraph of each page..."); | ||||||||||||
|
||||||||||||
// Get the active window. | ||||||||||||
let activeWindow = context.document.activeWindow; | ||||||||||||
activeWindow.load(); | ||||||||||||
await context.sync(); | ||||||||||||
|
||||||||||||
// Get the active pane. | ||||||||||||
let activePane = activeWindow.activePane; | ||||||||||||
activePane.load(); | ||||||||||||
await context.sync(); | ||||||||||||
|
||||||||||||
// Get all pages. | ||||||||||||
let pages: Word.PageCollection = activePane.pages; | ||||||||||||
pages.load(); | ||||||||||||
await context.sync(); | ||||||||||||
|
||||||||||||
// Get page index and paragraphs of each page. | ||||||||||||
for (let i = 0; i < pages.items.length; i++) { | ||||||||||||
// Get page index of the page. | ||||||||||||
let page = pages.items[i]; | ||||||||||||
page.load(); | ||||||||||||
await context.sync(); | ||||||||||||
|
||||||||||||
console.log(`Page index: ${page.index}`); | ||||||||||||
await context.sync(); | ||||||||||||
|
||||||||||||
// Get paragraphs of the page. | ||||||||||||
const range = page.getRange(); | ||||||||||||
range.load(); | ||||||||||||
let paragraphs: Word.ParagraphCollection = range.paragraphs; | ||||||||||||
paragraphs.load(); | ||||||||||||
await context.sync(); | ||||||||||||
|
||||||||||||
// Log the number of paragraphs on the page. | ||||||||||||
let paragraphsCount = paragraphs.items.length; | ||||||||||||
await context.sync(); | ||||||||||||
console.log(`Number of paragraphs: ${paragraphsCount}`); | ||||||||||||
// Log the first paragraph of the page. | ||||||||||||
let firstParagraph = paragraphs.items[0]; | ||||||||||||
firstParagraph.load(); | ||||||||||||
await context.sync(); | ||||||||||||
|
||||||||||||
console.log("First paragraph's text:", firstParagraph.text); | ||||||||||||
} | ||||||||||||
}); | ||||||||||||
} | ||||||||||||
|
||||||||||||
async function setup() { | ||||||||||||
await Word.run(async (context) => { | ||||||||||||
const body: Word.Body = context.document.body; | ||||||||||||
body.clear(); | ||||||||||||
body.insertBreak(Word.BreakType.page, Word.InsertLocation.end); | ||||||||||||
body.insertParagraph( | ||||||||||||
"Themes and styles also help keep your document coordinated. When you click design and choose a new Theme, the pictures, charts, and SmartArt graphics change to match your new theme. When you apply styles, your headings change to match the new theme.", | ||||||||||||
"End" | ||||||||||||
); | ||||||||||||
body.insertText( | ||||||||||||
"Save time in Word with new buttons that show up where you need them. To change the way a picture fits in your document, click it and a button for layout options appears next to it. When you work on a table, click where you want to add a row or a column, and then click the plus sign.", | ||||||||||||
"Start" | ||||||||||||
); | ||||||||||||
body.insertParagraph( | ||||||||||||
"Do you want to create a solution that extends the functionality of Word? You can use the Office Add-ins platform to extend Word clients running on the web, on a Windows desktop, or on a Mac.", | ||||||||||||
"Start" | ||||||||||||
); | ||||||||||||
body.paragraphs | ||||||||||||
.getLast() | ||||||||||||
.insertText( | ||||||||||||
"Use add-in commands to extend the Word UI and launch task panes that run JavaScript that interacts with the content in a Word document. Any code that you can run in a browser can run in a Word add-in. Add-ins that interact with content in a Word document create requests to act on Word objects and synchronize object state.", | ||||||||||||
"Replace" | ||||||||||||
); | ||||||||||||
}); | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Default helper for invoking an action and handling errors. | ||||||||||||
async function tryCatch(callback) { | ||||||||||||
try { | ||||||||||||
await callback(); | ||||||||||||
} catch (error) { | ||||||||||||
// Note: In a production add-in, you'd want to notify the user through your add-in's UI. | ||||||||||||
console.error(error); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
language: typescript | ||||||||||||
template: | ||||||||||||
content: |- | ||||||||||||
<section class="ms-Fabric ms-font-m"> | ||||||||||||
This sample demonstrates how to work with the pages, panes, and windows APIs. | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
</section> | ||||||||||||
<section class="ms-Fabric setup ms-font-m"> | ||||||||||||
<h3>Set up</h3> | ||||||||||||
<button id="setup" class="ms-Button"> | ||||||||||||
<span class="ms-Button-label">Add text</span> | ||||||||||||
</button> | ||||||||||||
</section> | ||||||||||||
<section class="ms-Fabric samples ms-font-m"> | ||||||||||||
<h3>Try it out</h3> | ||||||||||||
<button id="get-pages-selected-range" class="ms-Button"> | ||||||||||||
<span class="ms-Button-label">Get page info for selection</span> | ||||||||||||
</button> | ||||||||||||
<button id="get-pages-third-paragraph" class="ms-Button"> | ||||||||||||
<span class="ms-Button-label">Get the third paragraph's page info</span> | ||||||||||||
</button> | ||||||||||||
<button id="get-pages-enclosing-viewport" class="ms-Button"> | ||||||||||||
<span class="ms-Button-label">Get pages enclosing viewport</span> | ||||||||||||
</button> | ||||||||||||
<button id="get-all-pages" class="ms-Button"> | ||||||||||||
<span class="ms-Button-label">Get all pages</span> | ||||||||||||
</button> | ||||||||||||
</section> | ||||||||||||
language: html | ||||||||||||
style: | ||||||||||||
content: |- | ||||||||||||
section.samples { | ||||||||||||
margin-top: 20px; | ||||||||||||
} | ||||||||||||
|
||||||||||||
section.samples .ms-Button, section.setup .ms-Button { | ||||||||||||
display: block; | ||||||||||||
margin-bottom: 5px; | ||||||||||||
margin-left: 20px; | ||||||||||||
min-width: 80px; | ||||||||||||
} | ||||||||||||
language: css | ||||||||||||
libraries: | | ||||||||||||
https://appsforoffice.microsoft.com/lib/1/hosted/office.js | ||||||||||||
@types/office-js | ||||||||||||
|
||||||||||||
[email protected]/dist/css/fabric.min.css | ||||||||||||
[email protected]/dist/css/fabric.components.min.css | ||||||||||||
|
||||||||||||
[email protected]/client/core.min.js | ||||||||||||
@types/core-js | ||||||||||||
|
||||||||||||
[email protected] | ||||||||||||
@types/[email protected] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we're working with APIs is already implied since we're in Script Lab.