我正在使用谷歌電子表格爲我正在做的一些數字數據原型。有沒有辦法基本上將一個子集導出到文本文件?實際上,我打算做的是將我可以直接包含在另一個項目的構建中的文件導出。我可以從Google Spreadsheets腳本生成文件嗎?
那麼有沒有辦法生成一個文本文件下載?
我正在使用谷歌電子表格爲我正在做的一些數字數據原型。有沒有辦法基本上將一個子集導出到文本文件?實際上,我打算做的是將我可以直接包含在另一個項目的構建中的文件導出。我可以從Google Spreadsheets腳本生成文件嗎?
那麼有沒有辦法生成一個文本文件下載?
如果您有Google Apps帳戶,則可以使用DocsList.createFile()創建文本文件並將其保存在文檔列表中。
此tutorial的第3部分顯示瞭如何將CSV格式的電子表格的選定範圍保存爲文件列表中的文件。它可以很容易地修改以保存不同的格式。
我在Google Spreadsheet的某些欄目中顯示了我的項目的文字。 我把這script tutorial from Google和修改它只選擇一個特定的範圍(在下面的例子中是D4:D)。
它在您的Drive根文件夾中生成一個CSV文件。它仍然不下載文件 - 我現在正在處理這個文件。
希望它有幫助!
/*下面的代碼是從本教程的變形例:https://developers.google.com/apps-script/articles/docslist_tutorial#section3 */
/* The code below is a modification from this tutorial: https://developers.google.com/apps-script/articles/docslist_tutorial#section3 */
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var csvMenuEntries = [{name: "Save as CSV file", functionName: "saveAsCSV"}];
ss.addMenu("CSV", csvMenuEntries);
}
function saveAsCSV() {
// Name the file
fileName = "quests.csv";
// Convert the range data to CSV format
var csvFile = convertRangeToCsvFile_(fileName);
// Create a file in the root of my Drive with the given name and the CSV data
DriveApp.createFile(fileName, csvFile);
}
function convertRangeToCsvFile_(csvFileName) {
// Get from the spreadsheet the range to be exported
var rangeToExport = SpreadsheetApp.getActiveSpreadsheet().getRange("D4:D");
try {
var dataToExport = rangeToExport.getValues();
var csvFile = undefined;
// Loop through the data in the range and build a string with the CSV data
if (dataToExport.length > 1) {
var csv = "";
for (var row = 0; row < dataToExport.length; row++) {
for (var col = 0; col < dataToExport[row].length; col++) {
if (dataToExport[row][col].toString().indexOf(",") != -1) {
//dataToExport[row][col] = "\"" + dataToExport[row][col] + "\"";
dataToExport[row][col] = dataToExport[row][col];
}
}
// Join each row's columns
// Add a carriage return to end of each row, except for the last one
if (row < dataToExport.length-1) {
csv += dataToExport[row].join(",") + "\r\n";
}
else {
csv += dataToExport[row];
}
}
csvFile = csv;
}
return csvFile;
}
catch(err) {
Logger.log(err);
Browser.msgBox(err);
}
}
的已超出DocsList服務不再功能:https://developers.google.com/apps -script /日落 –