Trong project google sheet, google doc và apps script này, mình chia sẽ là “Tự động điền dữ liệu từ Google sheet sang Mẫu trên Google Doc” – Autofill google Doc template from google sheet. Nếu như bạn đã từng sử dụng mergmail trên MS Word thì ở đây nó cũng tương tự nhưng chúng ta thực hiện trên dịch vụ đám mây của Google. Bạn xem video hướng dẫn bên dưới và code mâu app script.
Các project khác bạn có thể xem thêm:
- Web App Script CSS JS | Tạo hiệu ứng Click button Nổ tung các mảnh giấy và chuyển link
- Web App Script CSS | Tạo button Liên hệ gồm 3 option đẹp mắt cho trang web
- Web App Script Webapp | Hiệu ứng hoa rơi – Form nhập liệu Gửi nội dung đến email
- Web App Script | Thanh trạng thái Status bar – Giá trị thể hiện theo điểm và label.
- Google sheet Apps script | Data Entry Form – Tự động đọc số tiền thành chữ ở trường input
- Google sheet Apps script | Cập nhật điểm lớp học – Theo danh sách lớp và Theo từng học sinh
- Google sheet, apps script Định dạng dấu phân cách hàng ngàn cho input
- Google sheet apps script | Chọn năm và kiểu biểu đồ để Load dữ liệu lên website
- Google sheet apps script | Scan QR code – Filter and get data table display on webapp
- Google sheet apps script Filter to get data to display on webapp, fill background color for data row
Mã “Code.gs”
function onOpen(){
const ui = SpreadsheetApp.getUi();
ui.createMenu('AutoFill Docs')
.addItem("Create New Docs", "createNewGoogleDocs")
.addToUi();
}
function createNewGoogleDocs() {
const googleDocTemplate = DriveApp.getFileById('1Z_bSOXrT7BswSVae9NDcCTsUQO9Aw4WEGEmQlFZjsj4');
const destinationFolder = DriveApp.getFolderById('12A-67fVuIm-ckK2SgY0WbvHqK_Xm6j5-')
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data')
const rows = sheet.getDataRange().getValues();
rows.forEach(function(row, index) {
if (index === 0) return;
if (row[5]) return;
const copy = googleDocTemplate.makeCopy(`Thư mời Ông (bà) ${row[0]} tham dự Hội nghị`, destinationFolder)
const doc = DocumentApp.openById(copy.getId())
const body = doc.getBody();
//const friendlyDate = new Date(row[3]).toLocaleDateString();
body.replaceText('{{Hovaten}}', row[0]);
body.replaceText('{{Diachi}}', row[1]);
body.replaceText('{{Donvi}}', row[2]);
body.replaceText('{{Dienthoai}}', row[3]);
body.replaceText('{{Email}}', row[4]);
doc.saveAndClose();
const url = doc.getUrl();
sheet.getRange(index +1, 6).setValue(url)
})
}