1 year ago
#376529
R ippy
Can I auto fill Google spreadsheet from Google Sheets source Using Google Apps Script?
I found a tutorial online which allows me to transform google sheet source file data to google doc which is extremely useful, however I would like to like to auto fill in in the GOOGLE SHEET instead, tried to modify the coding directly however unfortuantely didn't work, wondering if this is technically impossible to set variance value in google sheet or there's some other way to workaround?
youtube tutorial for auto filling google doc: https://www.youtube.com/watch?v=iLALWX0_OYs
below coding is from the tutorial reference -
function onOpen() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('AutoFill Docs');
menu.addItem('Create New Docs', 'createNewGoogleDocs')
menu.addToUi();
}
function createNewGoogleDocs() {
//This value should be the id of your document template that we created in the last step
const googleDocTemplate = DriveApp.getFileById('YOUR_FILE_ID_HERE');
//This value should be the id of the folder where you want your completed documents stored
const destinationFolder = DriveApp.getFolderById('YOUR_FOLDER_ID_HERE')
//Here we store the sheet as a variable
const sheet = SpreadsheetApp
.getActiveSpreadsheet()
.getSheetByName('Data')
//Now we get all of the values as a 2D array
const rows = sheet.getDataRange().getValues();
//Start processing each spreadsheet row
rows.forEach(function(row, index){
//Here we check if this row is the headers, if so we skip it
if (index === 0) return;
//Here we check if a document has already been generated by looking at 'Document Link', if so we skip it
if (row[5]) return;
//Using the row data in a template literal, we make a copy of our template document in our destinationFolder
const copy = googleDocTemplate.makeCopy(`${row[1]}, ${row[0]} Employee Details` , destinationFolder)
//Once we have the copy, we then open it using the DocumentApp
const doc = DocumentApp.openById(copy.getId())
//All of the content lives in the body, so we get that for editing
const body = doc.getBody();
//In this line we do some friendly date formatting, that may or may not work for you locale
const friendlyDate = new Date(row[3]).toLocaleDateString();
//In these lines, we replace our replacement tokens with values from our spreadsheet row
body.replaceText('{{First Name}}', row[0]);
body.replaceText('{{Last Name}}', row[1]);
body.replaceText('{{Position}}', row[2]);
body.replaceText('{{Hire Date}}', friendlyDate);
body.replaceText('{{Hourly Wage}}', row[4]);
//We make our changes permanent by saving and closing the document
doc.saveAndClose();
//Store the url of our new document in a variable
const url = doc.getUrl();
//Write that value back to the 'Document Link' column in the spreadsheet.
sheet.getRange(index + 1, 6).setValue(url)
})
}
I'm expecting the coding allows me to perform 3 functions -
- auto create excel once i run the code
- auto fill in excel with source information in the source excel
- return the google spreadsheet URL greated and store in the google source sheet
google-apps-script
google-sheets
autofill
0 Answers
Your Answer