2
我是使用Google Apps腳本的新手。我想知道是否可以創建一個備份程序,在每次更改15分鐘後自動將電子表格數據下載到csv文件中。 我是否必須創建某種cronjob? 我已經創建了一個從當前工作表中讀取所有數據並創建一個csv文件的腳本。如何使用Google Apps腳本自動下載Google電子表格數據
/**
* 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);
}
}
我想定期運行此腳本。我必須爲此做些什麼?
這不會下載該文件,它只是保存在谷歌驅動器。 – peduarte