code app script gọp nhiều file docs
Người anh em muốn gọp nhiều file Google Docs thành một file duy nhất phải không? Qua hiểu rồi. Qua sẽ viết cho người anh em một đoạn mã Google Apps Script giúp thực hiện việc này.
Dưới đây là đoạn mã:
function mergeDocuments(docIds, destinationDocId) {
var destinationDoc = DocumentApp.openById(destinationDocId);
var destinationBody = destinationDoc.getBody();
for (var i = 0; i < docIds.length; i++) {
var sourceDoc = DocumentApp.openById(docIds[i]);
var sourceBody = sourceDoc.getBody();
var totalElements = sourceBody.getNumChildren();
for (var j = 0; j < totalElements; ++j) {
var element = sourceBody.getChild(j).copy();
destinationBody.appendParagraph(element);
}
if (i < docIds.length - 1) {
// Add a page break between documents
destinationBody.appendPageBreak();
}
}
destinationDoc.saveAndClose();
}
Để sử dụng đoạn mã này, người anh em cần làm như sau:
docIds
thành danh sách ID của các tài liệu Google Docs mà người anh em muốn gộp, và destinationDocId
thành ID của tài liệu đích.Ví dụ:
function mergeMyDocuments() {
var docIds = ['docId1', 'docId2', 'docId3']; // Thay thế bằng ID của các tài liệu của người anh em
var destinationDocId = 'destinationDocId'; // Thay thế bằng ID của tài liệu đích
mergeDocuments(docIds, destinationDocId);
}
Chạy hàm mergeMyDocuments
để bắt đầu gộp các file.
Qua hy vọng đoạn mã này sẽ giúp người anh em giải quyết vấn đề của mình một cách hiệu quả. Nếu gặp khó khăn hay có thắc mắc gì, đừng ngần ngại hỏi qua!
Và đừng quên, hãy ghé thăm trituenhantao.io để khám phá thêm nhiều kiến thức thú vị về AI và công nghệ nhé!