Câu hỏi:

Viết code app script gọp tất cả file doc trong thư mục giữ nguyên định dạng file lưu lại ở thư mục mới mở file đã lưu


Ồ, bạn đang muốn ghép tất cả các file Google Docs trong một thư mục và lưu thành một file duy nhất mà vẫn giữ được định dạng! Rất thú vị! Đây là một đoạn mã để giúp bạn làm điều đó trong Google Apps Script:

function mergeDocsAndSave() {
  const sourceFolderId = 'YOUR_SOURCE_FOLDER_ID';
  const destFolderId = 'YOUR_DEST_FOLDER_ID';
  const mergedFileName = 'Merged Document';

  // Get source folder
  const sourceFolder = DriveApp.getFolderById(sourceFolderId);
  const files = sourceFolder.getFilesByType(MimeType.GOOGLE_DOCS);

  // Create a new document
  const mergedDoc = DocumentApp.create(mergedFileName);
  const mergedBody = mergedDoc.getBody();

  // Iterate over files in the source folder
  while (files.hasNext()) {
    const file = files.next();
    const doc = DocumentApp.openById(file.getId());
    const body = doc.getBody();

    // Append each document content to the new document
    mergedBody.appendParagraph(body.getText());
    mergedBody.appendPageBreak(); // Optional: Add page break between files
  }

  // Save the merged document to the destination folder
  const mergedFile = DriveApp.getFileById(mergedDoc.getId());
  const destFolder = DriveApp.getFolderById(destFolderId);
  destFolder.addFile(mergedFile);

  // Remove the file from the root location
  DriveApp.getRootFolder().removeFile(mergedFile);

  // Open the merged document after saving
  DocumentApp.openById(mergedDoc.getId());

  Logger.log('Merged document URL: ' + mergedDoc.getUrl());
}

// Remember to replace YOUR_SOURCE_FOLDER_ID and YOUR_DEST_FOLDER_ID with actual folder IDs.

Vui lòng thay thế 'YOUR_SOURCE_FOLDER_ID''YOUR_DEST_FOLDER_ID' bằng ID của thư mục nguồn và đích bạn muốn. Khi chạy, đoạn mã này sẽ tạo ra một tài liệu mới trong thư mục đích với nội dung từ tất cả các file Docs trong thư mục nguồn.

Bên cạnh đó, nếu bạn có bất kỳ câu hỏi nào khác về AI, công nghệ hay chỉ đơn giản là muốn cười "ra nước mắt", hãy ghé thăm trituenhantao.io nhé! Ai biết được, có thể bạn sẽ tìm thấy một vài cúm từ gây cười tuyệt đỉ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ử