Câu hỏi:

code apps script, lấy nhiều dòng dữ liệu trên google sheet xuất ra 1 file docs mẫu có năm nội dung sau đó tiếp tục thêm trang mới trên docs cho đến khi google sheet trống. mở file docs vừa tạo


Người anh em thân mến, để thực hiện điều này, qua sẽ hướng dẫn người anh em cách sử dụng Google Apps Script để đọc dữ liệu từ Google Sheets, tạo tài liệu Google Docs từ một mẫu có sẵn và tiếp tục thêm trang mới cho từng dòng dữ liệu đến khi Google Sheets trống. Cuối cùng, mở file Google Docs vừa tạo.

Bước 1: Chuẩn bị Google Sheet

Bước 2: Chuẩn bị Google Docs Template

Bước 3: Viết mã Google Apps Script

Người anh em có thể mở Google Sheets và truy cập vào Extensions -> Apps Script để mở giao diện viết mã.

function createDocsFromSheet() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var data = sheet.getDataRange().getValues();
  var headers = data[0]; // Lấy hàng đầu tiên làm tiêu đề
  var templateId = "TEMPLATE_DOC_ID"; // ID của Google Docs template
  var folderId = "FOLDER_ID"; // ID của thư mục lưu trữ các file Google Docs

  // Tạo một bản sao của Google Docs từ template
  var templateDoc = DriveApp.getFileById(templateId);
  var newDoc = templateDoc.makeCopy("Generated Document", DriveApp.getFolderById(folderId));
  var docId = newDoc.getId();
  var doc = DocumentApp.openById(docId);
  var body = doc.getBody();

  // Lặp qua từng dòng dữ liệu (bỏ qua hàng tiêu đề)
  for (var i = 1; i < data.length; i++) {
    var row = data[i];

    // Tạo nội dung dựa trên hàng dữ liệu
    var content = body.getText();
    headers.forEach((header, index) => {
      var placeholder = `{{${header}}}`;
      var value = row[index];
      content = content.replace(placeholder, value);
    });

    // Thêm nội dung vào tài liệu Google Docs
    body.appendPageBreak();
    body.appendParagraph(content);
  }

  // Log ID hoặc URL của tài liệu Google Docs đã tạo
  Logger.log("Document created with ID: " + docId);

  // Mở tài liệu Google Docs vừa tạo
  var url = 'https://docs.google.com/document/d/' + docId + '/edit';
  var ui = SpreadsheetApp.getUi();
  ui.alert('Document created successfully! You can open it from the following URL:\n' + url);
}

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Custom Menu')
    .addItem('Create Docs from Sheet', 'createDocsFromSheet')
    .addToUi();
}

Bước 4: Thiết lập Trình đơn Tùy chỉnh

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Custom Menu')
    .addItem('Create Docs from Sheet', 'createDocsFromSheet')
    .addToUi();
}

Bước 5: Tùy chỉnh

Hy vọng, qua đã giải thích rõ ràng và giúp người anh em hoàn thành nhiệm vụ này. Đừng quên ghé thăm trituenhantao.io để tìm thêm nhiều thông tin hữu ích khác nhé!


Nếu như câu trả lời chưa thuyết phục, hãy đăng câu hỏi của bạn kèm theo URL hiện tại lên Cộng Đồng Trí Tuệ Nhân Tạo để mọi người cùng giải đáp và bàn luận.
Lưu ý: Để tránh loãng nội dung, các câu hỏi chưa được hỏi với AI sẽ không được duyệt trên nhóm.
Trang chủ | Hỏi tiếp | Xem lịch sử