Tự động điền dữ liệu từ Google sheet sang Mẫu trên Google Doc
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:
- Googlesheet appscript – Hệ Thống Đăng Ký Hồ Sơ Trực Tuyến (Online Registration Portal)
- [Share Code] Biến Google Sheet thành Web App Tra Cứu Dự Án & Tài Liệu (Miễn Phí Hosting)
- Hệ Thống Quản Lý Phòng Game “Cloud-Native” với Google Apps Script
- Hệ Thống Điều Phối & Quản Lý Đội Xe Thông Minh (Web App)
- Biến Google Sheets thành Dashboard “Viễn Tưởng” phong cách Cyberpunk
- Google appscript | Hệ thống tìm thông tin và nhập liệu 2 Form-Tự động truy vấn và upload nhiều files
- Google sheet Webapp|Giáo viên chủ nhiệm quản lý điểm, thống kê xếp hạng chia sẻ cho phụ huynh
- Google sheet Webapp | Bản nâng cấp Tìm và Load Thông tin sinh viên có Hình ảnh và Bảng kết quả thi
- Google webapp | Form tìm, hiển thị kết quả học tập nhiều môn và in phiếu kết quả
- Danh mục các Bài học Google sheet Apps script Cơ bản
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)
})
}
