【问题标题】:how to automatically download Google spreadsheet data using Google Apps script如何使用 Google Apps 脚本自动下载 Google 电子表格数据
【发布时间】:2013-04-03 06:02:02
【问题描述】:

我是使用 Google Apps 脚本的新手。我想知道是否可以创建一个备份程序,在每次更改 15 分钟后自动将电子表格数据下载到 csv 文件中。 我必须创建某种 cronjob 吗? 我创建了一个以下脚本,它从当前工作表中读取所有数据并创建一个 csv 文件。

/**
 * Adds a custom menu to the active spreadsheet, containing a single menu item
 * for invoking the readRows() function specified above.
 * The onOpen() function, when defined, is automatically invoked whenever the
 * spreadsheet is opened.
 * For more information on using the Spreadsheet API, see
 * https://developers.google.com/apps-script/service_spreadsheet
 */
function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Download Data",
    functionName : "saveAsCSV"
  }];
  sheet.addMenu("Script Center Menu", entries);
};

function saveAsCSV() {
  // Prompts the user for the file name
  var fileName = Browser.inputBox("Save CSV file as (e.g. myCSVFile):");

  // Check that the file name entered wasn't empty
  if (fileName.length !== 0) {
    // Add the ".csv" extension to the file name
    fileName = fileName + ".csv";
    // Convert the range data to CSV format
    var csvFile = convertRangeToCsvFile_(fileName);
    // Create a file in the Docs List with the given name and the CSV data
    DocsList.createFile(fileName, csvFile);
  }
  else {
    Browser.msgBox("Error: Please enter a CSV file name.");
  }
}

function convertRangeToCsvFile_(csvFileName) {
  try {
    var csvFile = undefined;

    var sheet = SpreadsheetApp.getActiveSheet();
    var rows = sheet.getDataRange();
    var numRows = rows.getNumRows();
    var data = rows.getValues();

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

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

我想定期运行这个脚本。我该怎么办?

【问题讨论】:

    标签: javascript google-apps-script


    【解决方案1】:

    代码将相同。使用只需在电子表格上添加触发器。 只需转到脚本编辑器 -> 资源 -> 所有触发器 并添加一个新的触发器,使其成为时间驱动并选择间隔,就完成了。 :)

    function saveAsCSV() {
      // Prompts the user for the file name
      var fileName = SpreadsheetApp.getActiveSheet().getSheetName();
    
      // Check that the file name entered wasn't empty
      if (fileName.length !== 0) {
        // Add the ".csv" extension to the file name
        fileName = fileName + ".csv";
    
        // Convert the range data to CSV format
        var csvFile = convertRangeToCsvFile_(fileName);
        // Create a file in the Docs List with the given name and the CSV data
        DocsList.createFile(fileName, csvFile);
      }
      else {
        Browser.msgBox("Error: Please enter a CSV file name.");
      }
    }
    
    function convertRangeToCsvFile_(csvFileName) {
      try {
        var csvFile = undefined;
    
        var ss = SpreadsheetApp.getActiveSpreadsheet();
        var sheet = ss.getSheets()[10];
        var rows = sheet.getDataRange();
        var numRows = rows.getNumRows();
        var data = rows.getValues();
    
        // Loop through the data in the range and build a string with the CSV data
        if (data.length > 1) {
          var csv = "";
          for (var row = 0; row < data.length; row++) {
            for (var col = 0; col < data[row].length; col++) {
              if (data[row][col].toString().indexOf(",") != -1) {
                data[row][col] = "\"" + data[row][col] + "\"";
              }
            }
    
            // Join each row's columns
            // Add a carriage return to end of each row, except for the last one
            if (row < data.length-1) {
              csv += data[row].join(",") + "\r\n";
            }
            else {
              csv += data[row];
            }
          }
          csvFile = csv;
        }
        return csvFile;
      }
      catch(err) {
        Logger.log(err);
        Browser.msgBox(err);
      }
    }
    

    【讨论】:

    • 这不会下载文件,它只是保存在谷歌驱动器中。
    猜你喜欢
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 2023-01-13
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    相关资源
    最近更新 更多