在Google Docs中实现类似Word宏的文本查找选中脚本需求
Find & Select
(%%%) in Google Docs with a Custom Script I’ve got you covered! Here’s how to replicate your Word macro functionality in Google Docs using Apps Script. We’ll create a script that finds and selects your (%%%) placeholder, plus add a keyboard shortcut to trigger it—just like your old macro.
Basic Script: Find the First Occurrence
This script will locate the first instance of (%%%) and select it immediately:
function findAndSelectPlaceholder() { const doc = DocumentApp.getActiveDocument(); const body = doc.getBody(); // Escape parentheses since they're regex special characters const searchPattern = "\\(%%%\\)"; const match = body.findText(searchPattern); if (match) { const element = match.getElement(); const start = match.getStartOffset(); const end = match.getEndOffsetInclusive(); // Create a selection range and apply it const range = doc.newRange(); range.addElement(element, start, end); doc.setSelection(range); // Optional: Alert to confirm (remove if you don't want it) DocumentApp.getUi().alert("Found placeholder!"); } else { DocumentApp.getUi().alert("No more `(%%%)` placeholders left."); } }
Enhanced Script: Cycle Through All Occurrences
If you want to jump to the next placeholder each time you press the shortcut (instead of just the first), use this version—it remembers your last selected position:
function findNextPlaceholder() { const doc = DocumentApp.getActiveDocument(); const body = doc.getBody(); const searchPattern = "\\(%%%\\)"; const userProps = PropertiesService.getUserProperties(); // Retrieve last position from storage const lastElementId = userProps.getProperty("lastPlaceholderElement"); const lastEndOffset = parseInt(userProps.getProperty("lastPlaceholderEnd")) || 0; let match; // Try to find next match after last position if (lastElementId) { const lastElement = body.getChildById(lastElementId); if (lastElement) { match = body.findText(searchPattern, lastElement, lastEndOffset + 1); } } // If no match found after last position, start from the top if (!match) { match = body.findText(searchPattern); userProps.setProperty("lastPlaceholderElement", ""); userProps.setProperty("lastPlaceholderEnd", "0"); } if (match) { const element = match.getElement(); const start = match.getStartOffset(); const end = match.getEndOffsetInclusive(); // Save current position for next run userProps.setProperty("lastPlaceholderElement", element.getId()); userProps.setProperty("lastPlaceholderEnd", end.toString()); // Select the match const range = doc.newRange(); range.addElement(element, start, end); doc.setSelection(range); } else { DocumentApp.getUi().alert("No `(%%%)` placeholders found in this document."); userProps.deleteProperty("lastPlaceholderElement"); userProps.deleteProperty("lastPlaceholderEnd"); } }
How to Set This Up
- Open your Google Doc.
- Go to Extensions > Apps Script to open the script editor.
- Delete the default
myFunction()code and paste either script above. - Save the project (give it a name like "Placeholder Selector").
- Go back to your document, then navigate to Tools > Macros > Manage macros.
- Select the function you want to use (
findAndSelectPlaceholderorfindNextPlaceholder), click Assign shortcut, and pick a key combo (e.g., Ctrl+Shift+P). - Authorize the script when prompted (you’ll need to allow access to your document—follow the "Advanced" link to proceed safely).
Quick Notes
- The escaped
\\(and\\)in the search pattern are necessary because parentheses are special characters in regular expressions. If your placeholder was just%%%, you could use"%%%"instead. - The alert pop-ups are optional—feel free to remove those lines if you don’t want them.
- This works exactly like your Word macro: press the shortcut, and the next (or first)
(%%%)is selected, ready for you to replace with your data.
内容的提问来源于stack exchange,提问作者Jon Van Deurzen
相关产品推荐
相关产品推荐

