2016-08-22 96 views
0

我目前有一個谷歌電子表格,將創建一個CSV文件,並保存在我的谷歌驅動器,當觸發熄滅。我無法弄清楚如何在創建新的csv文件之前刪除現有的csv文件。當它保存新版本時,它只是製作另一個副本。我需要覆蓋以前的文件(或刪除舊的然後導出/寫入新的版本。)谷歌應用程序腳本導出和替換CSV文件

這裏是腳本來創建CSV文件:

function saveAsCSV() { 
    // Prompts the user for 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."); 
    } 
} 

我怎麼能覆蓋CSV文件每次腳本觸發器都會熄滅?

+0

的可能的複製[如何刪除/使用覆蓋CSV文件谷歌應用程序的腳本?](http://stackoverflow.com/questions/14965442/how-to-delete-overwrite-csv-file-using-google-apps-script) –

+0

我試着用這個解決方案,並不能得到它爲我工作 – cgrouge

回答

1

在使用file.setTrashed(true)編寫新文件之前,您可以刪除該文件。看到File Class documentation.

+0

我能夠在您提供的鏈接中找到解決方案。該文件被刪除,並取代它。謝謝! – cgrouge

1

鉛例如您可以先刪除該文件,使用File.setTrashed(true);,或者你可以簡單地改變文件的內容,使用File.setContent(csvFile);

+0

要更改文件的內容,請將其替換爲:「DriveApp.createFile(fileName,csvFile);」與您的解決方案:「File.setContent(csvFile);」? – cgrouge

+0

是的,除了你會得到'File' *實例*,而不是'DriveApp'這樣的單例。例如,'var f = DriveApp.getFile(id); f.setContent(CSV);' – rmhunter

相關問題