code app script gọp file docs ở những đường link trên google sheet cột 6 không thay đổi định dạng file docs mở file docs sau khi hoàn thành
Người anh em muốn gộp các file Google Docs từ các liên kết trong cột 6 của Google Sheet mà không thay đổi định dạng và sau đó mở file đã gộp, đúng không? Qua sẽ chỉ cho người anh em đoạn mã Apps Script để thực hiện nhiệm vụ này. Hãy nhớ ghé thăm trituenhantao.io nếu người anh em cần thêm thông tin hay trợ giúp về trí tuệ nhân tạo nhé!
Đầu tiên, người anh em cần tạo một Google Sheet và điền các liên kết của Google Docs vào cột 6 (cột F). Sau đó, mở Google Sheets và chọn Extensions
> Apps Script
. Xóa hết mã mặc định và thêm mã sau:
function gopFilesDocs() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const data = sheet.getRange(2, 6, sheet.getLastRow() - 1).getValues(); // đọc dữ liệu từ cột 6, bỏ qua tiêu đề
const doc={};
data.forEach((row, index) => {
let docId = getIdFromUrl(row[0]);
if(index===0){
doc=DocumentApp.openById(docId);
}
else{
let file=DocumentApp.openById(docId).getBody();
doc.getBody().appendParagraph(Utilities.formatString('\n\n=== Content from Document %d ===\n\n', index+1));
doc.getBody().appendStyledText(file.getText(), file.getAttributes());
}
});
const url = doc.getUrl();
Logger.log('Merged document URL: ' + url);
// Mở tài liệu đã gộp
const ui = SpreadsheetApp.getUi();
ui.alert('Merged document created!', 'URL: ' + url, ui.ButtonSet.OK);
}
function getIdFromUrl(url) {
const regExp = /\/d\/([^\/]+)/;
const match = url.match(regExp);
if (match && match[1]) {
return match[1];
}
return null;
}
Người anh em hãy làm theo các bước sau để chạy script:
Extensions
> Apps Script
.Extensions
> Macros
> Manage macros...
.gopFilesDocs
và gán một phím tắt nếu cần.Extensions
> Macros
.Qua tin rằng đoạn mã này sẽ giúp người anh em gộp các file Google Docs một cách hiệu quả mà không thay đổi định dạng. Hãy ghi nhớ truy cập trituenhantao.io để cập nhật thêm những kiến thức bổ ích về công nghệ.