以编程方式将 Google Sheet CSV 传输到 Dropbox
Programmatic Way to Transfer Google Sheet CSV to Dropbox
我试图每隔一小时左右在触发器上放置一个 Google Sheet 以导出 sheet 的内容并使用 Google 将它们上传到 Dropbox应用程序脚本。下面的代码成功获取了 sheet 的所有内容并在 Dropbox 中创建了一个新文件,但该文件始终为空。我认为这与我发送数据的方式有关,现在我认为它只是发送 csv 数据而不是实际文件。在将其发送到 Dropbox 之前,我一直在寻找如何将其转换为文件的方法,但我被卡住了:/。我是否需要先将此 csv 内容转换为 Google Drive 中的文件,还是有实时的方法?
function uploadToDropbox() {
try{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('sheet5');
var csvFile = convertRangeToCsvFile('testfile.csv', sheet)
var url = "https://content.dropboxapi.com";
var options = {
"hostname": url,
"method": "POST",
"encoding": "utf8",
"followRedirect": true,
"headers": {"Authorization": "Bearer XXXXXXXX",
"Content-Type": "application/octet-stream",
"Dropbox-API-Arg": '{"path": "/testfile.csv","mode": "overwrite","autorename": false,"mute": false,"strict_conflict": false}'
},
'muteHttpExceptions': true,
'body': csvFile
};
var response = UrlFetchApp.fetch(url + "/2/files/upload", options);
var responseCode = response.getResponseCode();
if(responseCode != 200) {throw 'Error: ' + responseCode + " - " + response}
var jsonResponse = JSON.parse(response.getContentText());
} catch(e) {
Logger.log(e)
}
}
function convertRangeToCsvFile(csvFileName, sheet) {
var activeRange = sheet.getDataRange();
try {
var data = activeRange.getValues();
var csvFile = undefined;
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] + "\"";
}
}
if (row < data.length-1) {
csv += data[row].join(",") + "\r\n";
}
else {
csv += data[row];
}
}
csvFile = csv;
}
return csvFile;
}
catch(err) {
Logger.log(err);
}
}
试试这个:
function uploadToDropbox() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet=ss.getSheetByName('sheet5');
var csvFile=DriveApp.getFileById(convertRangeToCsvFile('testfile.csv', sheet));//this is the file now do whatever you want with it. It contains the csv
var url="https://content.dropboxapi.com";
var options={
"hostname": url,
"method": "POST",
"encoding": "utf8",
"followRedirect": true,
"headers": {"Authorization": "Bearer XXXXXXXX",
"Content-Type": "application/octet-stream",
"Dropbox-API-Arg": '{"path": "/testfile.csv","mode": "overwrite","autorename": false,"mute": false,"strict_conflict": false}'
},
'muteHttpExceptions': true,
'body': csvFile
};
var response=UrlFetchApp.fetch(url + "/2/files/upload", options);
var responseCode=response.getResponseCode();
if(responseCode != 200) {throw 'Error: ' + responseCode + " - " + response}
var jsonResponse=JSON.parse(response.getContentText());
}
function convertRangeToCsvFile(csvFileName, sheet) {
var activeRange=sheet.getDataRange();
var data=activeRange.getValues();
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] + "\"";
}
}
if (row>data.length-1){csv += data[row].join(",") + "\r\n";
}else {csv += data[row];}
}
var file=DriveApp.getFilesByName('testFiles.csv').next();//assuming its the only one
file.setContent(csv);
}
return file.getId();
}
我试图每隔一小时左右在触发器上放置一个 Google Sheet 以导出 sheet 的内容并使用 Google 将它们上传到 Dropbox应用程序脚本。下面的代码成功获取了 sheet 的所有内容并在 Dropbox 中创建了一个新文件,但该文件始终为空。我认为这与我发送数据的方式有关,现在我认为它只是发送 csv 数据而不是实际文件。在将其发送到 Dropbox 之前,我一直在寻找如何将其转换为文件的方法,但我被卡住了:/。我是否需要先将此 csv 内容转换为 Google Drive 中的文件,还是有实时的方法?
function uploadToDropbox() {
try{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('sheet5');
var csvFile = convertRangeToCsvFile('testfile.csv', sheet)
var url = "https://content.dropboxapi.com";
var options = {
"hostname": url,
"method": "POST",
"encoding": "utf8",
"followRedirect": true,
"headers": {"Authorization": "Bearer XXXXXXXX",
"Content-Type": "application/octet-stream",
"Dropbox-API-Arg": '{"path": "/testfile.csv","mode": "overwrite","autorename": false,"mute": false,"strict_conflict": false}'
},
'muteHttpExceptions': true,
'body': csvFile
};
var response = UrlFetchApp.fetch(url + "/2/files/upload", options);
var responseCode = response.getResponseCode();
if(responseCode != 200) {throw 'Error: ' + responseCode + " - " + response}
var jsonResponse = JSON.parse(response.getContentText());
} catch(e) {
Logger.log(e)
}
}
function convertRangeToCsvFile(csvFileName, sheet) {
var activeRange = sheet.getDataRange();
try {
var data = activeRange.getValues();
var csvFile = undefined;
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] + "\"";
}
}
if (row < data.length-1) {
csv += data[row].join(",") + "\r\n";
}
else {
csv += data[row];
}
}
csvFile = csv;
}
return csvFile;
}
catch(err) {
Logger.log(err);
}
}
试试这个:
function uploadToDropbox() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet=ss.getSheetByName('sheet5');
var csvFile=DriveApp.getFileById(convertRangeToCsvFile('testfile.csv', sheet));//this is the file now do whatever you want with it. It contains the csv
var url="https://content.dropboxapi.com";
var options={
"hostname": url,
"method": "POST",
"encoding": "utf8",
"followRedirect": true,
"headers": {"Authorization": "Bearer XXXXXXXX",
"Content-Type": "application/octet-stream",
"Dropbox-API-Arg": '{"path": "/testfile.csv","mode": "overwrite","autorename": false,"mute": false,"strict_conflict": false}'
},
'muteHttpExceptions': true,
'body': csvFile
};
var response=UrlFetchApp.fetch(url + "/2/files/upload", options);
var responseCode=response.getResponseCode();
if(responseCode != 200) {throw 'Error: ' + responseCode + " - " + response}
var jsonResponse=JSON.parse(response.getContentText());
}
function convertRangeToCsvFile(csvFileName, sheet) {
var activeRange=sheet.getDataRange();
var data=activeRange.getValues();
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] + "\"";
}
}
if (row>data.length-1){csv += data[row].join(",") + "\r\n";
}else {csv += data[row];}
}
var file=DriveApp.getFilesByName('testFiles.csv').next();//assuming its the only one
file.setContent(csv);
}
return file.getId();
}