2016-08-23 52 views
0

我已經創建了一個表單,將所有字段數據填充到Google表單中,並且每次編輯電子表格時都會有一個腳本將電子表格導出到csv。但是,腳本僅在表單打開並在表單上編輯時運行。如果我提交編輯電子表格的表單,觸發器不會觸發,並且不會創建csv。Google App腳本導出CSV onEdit,而不是打開

在我當前的項目觸發器下,我有我的事件運行onedit。

當表單未打開時,如何使觸發器觸發?

function onOpen() { 
    var sheet = SpreadsheetApp.getActiveSpreadsheet(); 
    var entries = [{ 
    name : "Download Data", 
    functionName : "saveAsCSV" 
    }]; 
    sheet.addMenu("Script Center Menu", entries); 
}; 

function saveAsCSV() { 
    // Trash the previous csv file that was last updated. 
var files = DriveApp.getFilesByName('myCSVFile.csv'); 
while (files.hasNext()) { 
    var file = files.next(); 
    if (file.getLastUpdated()) { 
    file.setTrashed(true); 
    } 
} 
    // Creates the file name 
    var fileName = ("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 
    DriveApp.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); 
    } 
} 
+0

嘗試寫'onFormSubmit()'函數的觸發器 – iJay

+0

即使這是一個外部表單,這個工作是否會起作用?不是Google表單? – cgrouge

+0

我會嘗試更改'var sheet = SpreadsheetApp.getActiveSpreadsheet();'for openById – Harold

回答

0

從上面的評論獲得幫助,並調整我的觸發器後,我有一個解決方案。

下面是腳本:

function onEdit() { 
    var sheet = SpreadsheetApp.openById("id"); 
    var entries = [{ 
    name : "Download Data", 
    functionName : "saveAsCSV" 
    }]; 
    sheet.addMenu("Script Center Menu", entries); 
}; 

function saveAsCSV() { 
    // Trash the previous csv file that was last updated. 
var files = DriveApp.getFilesByName('myCSVFile.csv'); 
while (files.hasNext()) { 
    var file = files.next(); 
    if (file.getLastUpdated()) { 
    file.setTrashed(true); 
    } 
} 
    // Creates the file name 
    var fileName = ("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 
    DriveApp.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); 
    } 
} 

節省我需要我的觸發從onEdit更改爲定時觸發腳本文件之後。資源>所有觸發器我刪除了當前的saveAsCSV函數並創建了一個新的函數。然後選擇時間驅動,分鐘計時器,每分鐘。

現在當工作表關閉時,函數將運行並每分鐘創建一個新的CSV文件。