13

Tôi đang sử dụng bảng tính google để tạo mẫu dữ liệu số cho một số thứ tôi đang làm. Có cách nào về cơ bản xuất khẩu một tập con vào một tập tin văn bản? Có hiệu quả, những gì tôi đang định làm là xuất một tệp mà tôi có thể đưa trực tiếp vào bản dựng cho một dự án khác.Tôi có thể tạo tệp từ Tập lệnh Bảng tính Google không?

Vậy có cách nào để tạo tệp văn bản để tải xuống không?

Trả lời

12

Nếu bạn có tài khoản Google Apps, bạn có thể sử dụng DocsList.createFile() để tạo tệp văn bản và lưu tệp đó vào danh sách tài liệu của mình.

Phần 3 của số tutorial này cho biết cách lưu phạm vi bảng tính đã chọn dưới dạng tệp trong danh sách tài liệu của bạn ở định dạng CSV. Nó có thể được sửa đổi khá dễ dàng để lưu ở định dạng khác.

+0

Dịch vụ DocsList không còn chức năng: https://developers.google.com/apps -script/hoàng hôn –

1

Tôi có văn bản dự án của tôi trong một số cột của Bảng tính Google. Tôi lấy số này script tutorial from Google và sửa đổi nó để chỉ chọn một phạm vi cụ thể (trong ví dụ dưới đây là D4: D).

Nó tạo tệp CSV trong thư mục gốc Drive của bạn. Nó vẫn không tải về tập tin - Tôi đang làm việc trên đó ngay bây giờ.

Hy vọng điều đó sẽ hữu ích!

/* Đoạn code dưới đây là một thay đổi từ hướng dẫn này: https://developers.google.com/apps-script/articles/docslist_tutorial#section3 */

/* The code below is a modification from this tutorial: https://developers.google.com/apps-script/articles/docslist_tutorial#section3 */ 
 

 
function onOpen() { 
 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
 
    var csvMenuEntries = [{name: "Save as CSV file", functionName: "saveAsCSV"}]; 
 
    ss.addMenu("CSV", csvMenuEntries); 
 
} 
 

 
function saveAsCSV() { 
 
    
 
    // Name the file 
 
    fileName = "quests.csv"; 
 
    // Convert the range data to CSV format 
 
    var csvFile = convertRangeToCsvFile_(fileName); 
 
    // Create a file in the root of my Drive with the given name and the CSV data 
 
    DriveApp.createFile(fileName, csvFile); 
 
} 
 

 
function convertRangeToCsvFile_(csvFileName) { 
 
    // Get from the spreadsheet the range to be exported 
 
    var rangeToExport = SpreadsheetApp.getActiveSpreadsheet().getRange("D4:D"); 
 
    
 
    try { 
 
    var dataToExport = rangeToExport.getValues(); 
 
    var csvFile = undefined; 
 

 
    // Loop through the data in the range and build a string with the CSV data 
 
    if (dataToExport.length > 1) { 
 
     var csv = ""; 
 
     for (var row = 0; row < dataToExport.length; row++) { 
 
     for (var col = 0; col < dataToExport[row].length; col++) { 
 
      if (dataToExport[row][col].toString().indexOf(",") != -1) { 
 
      //dataToExport[row][col] = "\"" + dataToExport[row][col] + "\""; 
 
      dataToExport[row][col] = dataToExport[row][col]; 
 
      } 
 
     } 
 

 
     // Join each row's columns 
 
     // Add a carriage return to end of each row, except for the last one 
 
     if (row < dataToExport.length-1) { 
 
      csv += dataToExport[row].join(",") + "\r\n"; 
 
     } 
 
     else { 
 
      csv += dataToExport[row]; 
 
     } 
 
     } 
 
     csvFile = csv; 
 
    } 
 
    return csvFile; 
 
    } 
 
    catch(err) { 
 
    Logger.log(err); 
 
    Browser.msgBox(err); 
 
    } 
 
}