I want to create a connection between my Google Sheets and Google Docs so that when I update a cell in my spreadsheet, the corresponding text in my document gets updated automatically.
I’ve been working on this script but it’s not functioning properly and I’m not completely confident about the syntax I’m using.
var spreadsheet = SpreadsheetApp.openById("");
var worksheet = spreadsheet.getSheetByName('Sheet1');
var cellRange = worksheet.getRange('B3');
var cellValue = cellRange.getValue();
var document = DocumentApp.openById("");
const docBody = document.getBody();
docBody.replaceText('{{UserName}}', cellValue);
Can someone help me figure out what’s wrong with this approach? I’m trying to replace placeholder text in my Google Doc with actual values from specific cells in my Google Sheet.
The script looks fine structurally, but you’re hitting execution issues. First, make sure you actually filled in the document and spreadsheet IDs between those empty quotes - that’s usually the problem. Also check if you’re running this as a standalone script or bound to one of your files. I’ve found binding the script to the spreadsheet works way better for this stuff. Double-check that your placeholder text {{UserName}} exists exactly like that in your document - formatting can screw up the matching. Throw in some console.log statements to see what values you’re pulling from the sheet before trying to replace anything.
Had the same issues when I started with this automation stuff. Main problem is the script won’t run automatically when you change a cell - you’ve got to trigger it somehow. Either set up an installable trigger with ScriptApp.newTrigger() or run it manually each time. Also, replaceText() is case sensitive and breaks if your placeholder has any formatting. I always keep placeholders as plain text now. One more thing - test with a copy first because replaceText() will replace ALL instances of that pattern in the entire document.
your code’s fine, but your missing document.saveAndClose(); at the end. also double-check your permissions on both files and make sure the placeholder text matches exactly - including those curly braces.