Javascript 如何使用谷歌应用程序脚本自动下载谷歌电子表格数据
我是一个使用谷歌应用程序脚本的新手。我想知道是否可以创建一个备份程序,在每次更改15分钟后自动将电子表格数据下载到csv文件中。 我必须创建某种类型的cronjob吗? 我创建了一个脚本,它读取当前工作表中的所有数据并创建一个csv文件Javascript 如何使用谷歌应用程序脚本自动下载谷歌电子表格数据,javascript,google-apps-script,Javascript,Google Apps Script,我是一个使用谷歌应用程序脚本的新手。我想知道是否可以创建一个备份程序,在每次更改15分钟后自动将电子表格数据下载到csv文件中。 我必须创建某种类型的cronjob吗? 我创建了一个脚本,它读取当前工作表中的所有数据并创建一个csv文件 /** * Adds a custom menu to the active spreadsheet, containing a single menu item * for invoking the readRows() function specifie
/**
* 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);
}
}
/**
*将自定义菜单添加到活动电子表格,其中包含单个菜单项
*用于调用上面指定的readRows()函数。
*定义onOpen()函数后,每当
*电子表格已打开。
*有关使用电子表格API的更多信息,请参见
* https://developers.google.com/apps-script/service_spreadsheet
*/
函数onOpen(){
var sheet=SpreadsheetApp.getActiveSpreadsheet();
变量项=[{
名称:“下载数据”,
函数名:“saveAsCSV”
}];
sheet.addMenu(“脚本中心菜单”,条目);
};
函数saveAsCSV(){
//提示用户输入文件名
var fileName=Browser.inputBox(“将CSV文件另存为(例如myCSVFile):”;
//检查输入的文件名是否为空
如果(fileName.length!==0){
//将“.csv”扩展名添加到文件名
文件名=文件名+“.csv”;
//将范围数据转换为CSV格式
var csvFile=convertRangeToCsvFile(文件名);
//使用给定名称和CSV数据在文档列表中创建一个文件
创建文件(文件名,csvFile);
}
否则{
msgBox(“错误:请输入CSV文件名”);
}
}
函数convertRangeToCsvFile(csvFileName){
试一试{
var csvFile=未定义;
var sheet=SpreadsheetApp.getActiveSheet();
var rows=sheet.getDataRange();
var numRows=rows.getNumRows();
var data=rows.getValues();
//循环遍历范围中的数据,并使用CSV数据构建字符串
如果(data.length>1){
var csv=“”;
对于(变量行=0;行
我想定期运行这个脚本。我必须为此做些什么?代码将是相同的。只需在电子表格上添加触发器即可。 只需进入脚本编辑器->资源->所有触发器 并添加一个新触发器,使其成为时间驱动的,然后选择interval(间隔),即可完成操作。:) 函数saveAsCSV(){ //提示用户输入文件名 var fileName=SpreadsheetApp.getActiveSheet().getSheetName(); //检查输入的文件名是否为空 如果(fileName.length!==0){ //将“.csv”扩展名添加到文件名 文件名=文件名+“.csv”; //将范围数据转换为CSV格式 var csvFile=convertRangeToCsvFile(文件名); //使用给定名称和CSV数据在文档列表中创建一个文件 创建文件(文件名,csvFile); } 否则{ msgBox(“错误:请输入CSV文件名”); } } 函数convertRangeToCsvFile(csvFileName){ 试一试{ var csvFile=未定义; var ss=SpreadsheetApp.getActiveSpreadsheet(); var sheet=ss.getSheets()[10]; var rows=sheet.getDataRange(); var numRows=rows.getNumRows(); var data=rows.getValues(); //循环遍历范围中的数据,并使用CSV数据构建字符串 如果(data.length>1){ var csv=“”; 对于(变量行=0;行
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);
}
}