如何在Google Docs中实现段落自动复制粘贴至指定位置?
Absolutely! You can absolutely automate this task using Google Apps Script—it’s the Docs equivalent of the macros you’re already using in Sheets. Here’s a straightforward, customizable solution to get you up and running:
Step 1: Open the Script Editor
Open your target Google Doc, then go to Extensions > Apps Script to launch the script editor (it’s a mini code editor built right into Google Workspace).
Step 2: Paste the Custom Script
Replace the default myFunction() code with the following. I’ve added comments to help you tweak it to fit your exact document:
function copyParagraphToPresetLocations() { const doc = DocumentApp.getActiveDocument(); const body = doc.getBody(); // -------------------------- // 1. Pick your source paragraph to copy // Option A: Use paragraph index (starts at 0 for the first paragraph) const sourceParagraph = body.getParagraphs()[2]; // Change this number to match your target paragraph // Option B: Find by text (great if your paragraph has a unique phrase) // const searchText = "Your exact paragraph text here"; // const sourceElement = body.findText(searchText); // if (sourceElement) { // const sourceParagraph = sourceElement.getElement().getParent(); // } else { // DocumentApp.getUi().alert("Source paragraph not found!"); // return; // } // -------------------------- // Make a copy of the paragraph (preserves all formatting!) const copiedParagraph = sourceParagraph.copy(); // -------------------------- // 2. Define where to paste the copied paragraph // Example 1: Paste after specific paragraph indices const targetPositions = [5, 8, 12]; // Adjust these numbers to your desired paste spots targetPositions.forEach(pos => { body.insertParagraph(pos + 1, copiedParagraph.copy()); }); // Example 2: Paste at bookmarks (cleaner if your doc layout changes) // First, set bookmarks in your doc: Select a spot > Insert > Bookmark > Name it // const bookmarkNames = ["pasteLocation1", "pasteLocation2"]; // bookmarkNames.forEach(name => { // const bookmark = doc.getBookmark(name); // if (bookmark) { // bookmark.getPosition().insertParagraph(copiedParagraph.copy()); // } else { // console.log(`Bookmark ${name} not found`); // } // }); // -------------------------- // Pop up a confirmation when done DocumentApp.getUi().alert("Done! Paragraph copied to all preset locations."); }
Step 3: Customize the Script to Your Doc
- Set the source paragraph: Choose either Option A (by index) or Option B (by text search) and update the values to match your document.
- Set paste locations: Pick between pasting at specific paragraph indices or bookmarks. Bookmarks are more reliable if your doc’s content shifts often.
Step 4: Test the Script
Click the run button (▶️) in the script editor. The first time you run it, you’ll need to authorize the script (follow the prompts—Google will warn it’s an unverified app, but this is safe since you wrote it yourself).
Optional: Add a Quick Access Menu
To avoid opening the script editor every time, add a custom menu to your Doc’s toolbar. Just add this code below the main function:
function onOpen() { const ui = DocumentApp.getUi(); ui.createMenu("Auto Copy Tools") .addItem("Copy Paragraph to Presets", "copyParagraphToPresetLocations") .addToUi(); }
Now every time you open the doc, you’ll see an "Auto Copy Tools" menu at the top—just click the option to run your script instantly.
Quick Notes
- The script preserves all formatting (fonts, colors, bullet points, etc.) from the original paragraph. If you only want plain text, replace
sourceParagraph.copy()withsourceParagraph.getText(). - If you need to update the source paragraph or paste locations later, just head back to the script editor and tweak the values.
内容的提问来源于stack exchange,提问作者Simon Marty

