How to sync Google Sheets cell data with Google Docs placeholder text

I want to create a connection between my Google Sheets and Google Docs so that when I update a cell value in the spreadsheet, it automatically updates the corresponding text in my document. I’m trying to use Google Apps Script for this but my code isn’t working properly. Here’s what I’ve attempted so far:

var spreadsheet = SpreadsheetApp.openById("");

var worksheet = spreadsheet.getSheetByName('Sheet1');
var cell = worksheet.getRange('B3'); 
var cellValue = cell.getValue();

var document = DocumentApp.openById("");

const docBody = document.getBody();
docBody.replaceText('{{UserName}}', cellValue);

I think there might be an issue with how I’m getting the cell value or maybe the way I’m trying to replace the text. Can someone help me figure out what’s wrong with my approach?

your code’s almost there! just add document.saveAndClose() after the replaceText line. also, make sure there are no extra spaces around {{UserName}} in your doc - that often breaks things.

I hit the same issues when setting this up. The replaceText method only works while the script runs - if you want ongoing sync, you’ll need to set up a trigger. Add an installable trigger with ScriptApp.newTrigger() that fires when your spreadsheet changes. Also double-check your document and spreadsheet IDs in the openById functions. I wasted hours debugging once because I’d copied the wrong ID from the URL. And heads up - placeholder text matching is case-sensitive, so your document needs to match exactly what you’re searching for.