是的,您可以使用 Google 应用脚本 [1] 实现该过程。
使用电子表格服务 [2] 和 [3] 中的类的方法,您可以获得具有电子表格 ID(打开 Google 表格时出现在 url 中的字母数字 ID)的表格数据.然后循环遍历数据,解析成csv格式的字符串。
使用 csv 字符串,您可以创建一个 blob 对象 [4],该对象将使用 fetch 方法 [5] 通过发布请求发送到服务器。
要使您的代码自动运行,您可以使用手动触发器,例如,将它们设置为每分钟运行一次或根据需要运行 [6]。
您必须设置您的服务器应用程序以接收发布请求并在应用脚本中设置请求 url(以https://example.com/post 为例)。下面是我测试的代码,直到获得 csvBlob 变量:
function myFunction() {
var ss = SpreadsheetApp.openById("SpreadsheetID");
var sheet = ss.getSheets()[0];
// This represents ALL the data
var range = sheet.getDataRange();
var values = range.getValues();
var csvStr = "";
// This creates a string of the spreadsheet in CSV format with a trailing comma
for (var i = 0; i < values.length; i++) {
var row = "";
for (var j = 0; j < values[i].length; j++) {
if (values[i][j]) {
row = row + values[i][j];
row = row + ",";
row = row.substring(0, (row.length-1));
csvStr += row + "\n";
}
//creates de Blob of the csv file
var csvBlob = Utilities.newBlob(csvStr, 'text/csv', 'example.csv');
Logger.log(csvBlob.getDataAsString());
//make a post request to the server (I didn't test this part)
var formData = {
'name': 'Bob Smith',
'email': 'bob@example.com',
'file': csvBlob
};
var options = {
'method' : 'post',
'payload' : formData
};
UrlFetchApp.fetch('https://example.com/post', options);
}
[1]https://script.google.com/home
[2]https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app
[3]https://developers.google.com/apps-script/reference/spreadsheet/sheet
[4]https://developers.google.com/apps-script/reference/utilities/utilities#newBlob(Byte,String,String)
[5]https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app
[6]https://developers.google.com/apps-script/guides/triggers/installable