tôi có đoạn code : function luuTheoNgay() { // ID của thư mục trên Google Drive để lưu…
- User
tôi có đoạn code : 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);
// 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()); // Sao chép viền từ ô gốc sang ô mới var border = sourceCell.getBorder(); if (border) { targetCell.setBorder( border.top, border.right, border.bottom, border.left, border.color, border.style ); } }} // Mở thư mục và di chuyển file vào đó var folder = DriveApp.getFolderById(folderId); var file = DriveApp.getFileById(tempSpreadsheet.getId()); file.moveTo(folder);
// Thông báo hoàn thành SpreadsheetApp.getUi().alert("Đã lưu file '" + fileName + "' vào thư mục DSthucphamDV."); báo lỗi TypeError: sourceCell.hasBorder is not a function hãy sửa lỗi trên mà không ảnh hưởng đến code chung
- Assistant