function luuTheoNgay() { // ID của thư mục trên Google Drive để lưu file vào var…
- User
function luuTheoNgay() { // ID của thư mục trên Google Drive để lưu file vào var folderId = "16Ikte5M5fehDS8mQOX-zRNvg4GFBFIAv"; // Thay YOUR_FOLDER_ID bằng ID của thư mục "DSthucphamDV"
// Mở spreadsheet hiện tại và chọn sheet 'Nhapthucpham' var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheetByName("Nhapthucpham");
// Lấy tên file từ giá trị ở ô L4 var fileName = sheet.getRange("L4").getValue(); if (!fileName) { SpreadsheetApp.getUi().alert("Ô L4 trống. Vui lòng nhập tên file."); return; } // Chỉ lấy dữ liệu từ cột A đến cột H và chỉ lấy giá trị var dataRange = sheet.getRange("A1:H" + sheet.getLastRow()); var data = dataRange.getValues();
// Tạo một spreadsheet mới tạm thời để lưu dữ liệu var tempSpreadsheet = SpreadsheetApp.create(fileName); var tempSheet = tempSpreadsheet.getActiveSheet();
// Dán dữ liệu vào bảng tính tạm thời tempSheet.getRange(1, 1, data.length, data[0].length).setValues(data); tempSheet.setName("Nhapthucpham"); // Đặt tên cho sheet mới là "Nhapthucpham"
// Sao chép định dạng từng ô từ sheet gốc sang sheet tạm thời for (var r = 1; r <= data.length; r++) { for (var c = 1; c <= data[0].length; c++) { var sourceCell = sheet.getRange(r, c); var targetCell = tempSheet.getRange(r, c);
code// Sao chép màu nền, font chữ, kích thước chữ, màu chữ, kiểu in đậm và nghiêng, căn lề targetCell.setBackground(sourceCell.getBackground()); targetCell.setFontFamily(sourceCell.getFontFamily()); targetCell.setFontSize(sourceCell.getFontSize()); targetCell.setFontWeight(sourceCell.getFontWeight()); targetCell.setFontColor(sourceCell.getFontColor()); targetCell.setFontStyle(sourceCell.getFontStyle()); targetCell.setHorizontalAlignment(sourceCell.getHorizontalAlignment()); targetCell.setVerticalAlignment(sourceCell.getVerticalAlignment()); // *** Loại bỏ việc sao chép viền vì không có phương thức getBorder *** /* var border = sourceCell.getBorder(); if (border) { targetCell.setBorder( border.top, border.right, border.bottom, border.left, border.color, border.style ); } */ }}
- Assistant