将Google表格CSV传输到Dropbox的程序化方法

问题描述 投票:1回答:1

我试图每隔一个小时左右将Google表格放在触发器上,以导出表格的内容,然后使用Google Apps脚本将其上传到Dropbox。下面的代码成功获取了所有工作表的内容并在Dropbox中创建了一个新文件,但是该文件始终为空。我认为这与我发送数据的方式有关,现在我认为它只是发送csv数据,而不是实际文件。我一直在努力寻找如何将其转换为文件,然后再将其发送到Dropbox的方法,但是我陷于:/我需要首先将此csv内容转换为Google云端硬盘中的文件,还是可以实时进行?

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);
  }
}
csv google-apps-script google-sheets dropbox
1个回答
0
投票
Try this:

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();
}
© www.soinside.com 2019 - 2024. All rights reserved.