viết code apps script , xuất nhiều dòng dữ liệu từ google sheet sang file docs nhiều trang sau đó mở file docs
Người anh em thân mến, qua rất vui lòng hướng dẫn người anh em cách thực hiện điều này. Đầu tiên, chúng ta sẽ cần một Google Sheet với dữ liệu mà chúng ta muốn xuất sang Google Docs. Sau đó, chúng ta sẽ viết một script trong Google Apps Script để thực hiện công việc này. Hãy bắt đầu:
Tạo Google Sheet và Google Docs:
Mở Google Apps Script:
Extensions
-> Apps Script
.Viết mã script:
function exportSheetToDoc() {
// Lấy Spreadsheet hiện tại và Sheet đầu tiên.
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheets()[0];
// Lấy toàn bộ dữ liệu từ Sheet.
var data = sheet.getDataRange().getValues();
// Tạo một tài liệu Google Docs mới.
var doc = DocumentApp.create('Exported Data');
var body = doc.getBody();
// Thêm dữ liệu từ Sheet vào Docs, mỗi hàng một đoạn.
for (var i = 0; i < data.length; i++) {
var row = data[i];
body.appendParagraph(row.join(' '));
// Tách trang sau mỗi 50 hàng (tùy chọn, người anh em có thể điều chỉnh số này).
if ((i + 1) % 50 === 0) {
body.appendPageBreak();
}
}
// Lấy URL của Docs mới tạo.
var docUrl = doc.getUrl();
// Mở tài liệu Google Docs mới tạo.
var htmlOutput = HtmlService.createHtmlOutput('<a href="' + docUrl + '" target="_blank">Mở tài liệu</a>');
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Tài liệu đã được tạo');
}
Ctrl + S
.Run
để chạy script.Khi script chạy, dữ liệu từ Google Sheet sẽ được xuất sang Google Docs và tài liệu sẽ mở ra để người anh em có thể xem.
Qua hy vọng rằng điều này sẽ giúp ích cho người anh em. Nếu cần thêm thông tin, người anh em có thể truy cập trituenhantao.io để tìm hiểu thêm.