1

我尝试使用 Google Apps(脚本)通过 API 上传 .csv 文件。

我在 API URL 的一部分中给出了以下属性。

"ZOHO_FILE":file.csv

有没有办法在 Google Apps Script 中创建 .csv 文件?

如果可能,请告诉我们,如何在 Google Apps 中创建 .csv 文件?

对不起我的英语不好 :-(

4

2 回答 2

3

您可以使用它来转换数据范围:

function convertRangeToCsvFile(dataRange) {

  try {
    var data = dataRange.getValues();

    var csvFile = undefined;

    // Loop through the data in the range and build a string with the CSV data
    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] + "\"";
          }
        }

        // Join each row's columns
        // Add a carriage return to end of each row, except for the last one
        if (row < data.length-1) {
          csv += data[row].join(",") + "\r\n";
        }
        else {
          csv += data[row];
        }
      }
      csvFile = csv;
    }
    return csvFile;
  }
  catch(err) {
    Logger.log(err);
    Browser.msgBox(err);
  }
}

或者这个下载整张纸

function downloadSpreadsheet(){
  //This function generates a pdf of your current spreadsheet and emails it to yourself as attachment
  //Make sure your spreadsheet is not too big. The pdf size tends to be 200kb/page and if too large
  //if the pdf is too large the urlFetch might timeout

  var AUTH_TOKEN = "xxxxxxxxxxxxxx"; //Enter your AUTH_TOKEN
  //You can receive it from https://appscripts.appspot.com/getAuthToken

  var ssID=SpreadsheetApp.getActiveSpreadsheet().getId()+"&gid="+SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getSheetId();
  var url = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key="
      + ssID + "&exportFormat=csv"; 
  //Add &gid=x at the end of above url if you only want a particular sheet
  //gid of a sheet can be obtained by the undocumented function getSheetId()
  //ex: SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getSheetId();
  //exportFormat=xls did not work when I tried. I dont know why

  var auth = "AuthSub token=\"" + AUTH_TOKEN + "\"";

  var res = UrlFetchApp.fetch(url, {headers: {Authorization: auth}});
  var content=res.getContentText();
  return content
}
于 2012-05-24T10:36:50.253 回答
0

我希望您要求提供 Apps 脚本;

下面文章的第 2 节和第 3 节讲述了如何导入和导出 CSV

https://developers.google.com/apps-script/articles/docslist_tutorial#section2

于 2012-05-17T11:04:03.240 回答