将 csv 文件发布到网络
显然IMPORTDATA
仅适用于发布到网络的文档。让它为你工作的两种方法是:
- 将您的 csv 文件存储在公共 Google 云端硬盘文件夹中 https://productforums.google.com/forum/#!topic/docs/PkCViKt56Wo
- 以其他方式在网络上托管您的 csv 文件 - 我想您需要在您的计算机上使用 csv 文件运行服务器。
但是,这涉及将您的 csv 数据公开给连接到互联网的任何人(可能是您不想要的)
保持 csv 文件的私密性
如果您希望数据保持私密性,您可以编写一个脚本来定期将 csv 数据加载到电子表格中。
Google 提供服务调用谷歌应用脚本 https://developers.google.com/apps-script/overview(GAS),它允许您用 JavaScript 编写脚本,以在 Google Drive 中执行更复杂的任务。
在 GAS 中,您可以创建称为可安装触发器 https://developers.google.com/apps-script/guides/triggers/installable您可以将其设置为按设定的时间间隔定期运行。
以下是我如何编写上传 csv 数据的脚本的基本布局:
function timedTrigger() {
// list the csv file ids
var baseballScoresFileId = "123231jshu231731";
var donutPricesFileId = "984732ageyn555646";
// get the spreadsheet by it's id
var ss = SpreadsheetApp.openById("the spreadsheet id");
// get the sheets you want to print the data to
var baseballSheet = ss.getSheetByName("Baseball Scores");
var donutsSheet = ss.getSheetByName("Donuts Scores");
// print the data to the first row first columnscript
printCsvData(baseballScoresFileId, baseballSheet, 1, 1);
printCsvData(donutPricesFileId, donutsSheet, 5, 2); // prints to B5
}
// This function loads the data from a csv fileId
// and prints it to the sheet starting at the cell
// located at (row, col)
// It works just like IMPORTDATA except you specify the row and col in the
function printCsvData(fileId, sheet, row, col) {
// get data from file
var data = DriveApp.getFileById(fileId).getBlob().getDataAsString();
// parse the csv string into two dimensional array
var values = parseCsvData(data);
// print the values into the range starting at row, col
sheet.getRange(row, col, values.length, values[0].length).setValues(values);
}
// This function converts a string of comma
// separated values and converts them into
// a two dimensional array
function parseCsv(string) {
var values = [];
// ...
return values;
}
我不太确定您的 CSV 文件格式如何;我最好的猜测是:
function parseCSV(string) {
return string.split("\n").map(function (row) {
return row.split(",");
});
}
您可能需要担心额外的空白。
如果您不熟悉 JavaScript 或编程,这将是一个很好的学习机会。但是,如果加载 csv 文件不是迫切需要,请注意,您可能需要 5-20 小时来学习如何设置脚本。不管怎样,祝你好运!