google脚本从ftp导入csv到google驱动器

google脚本从ftp导入csv到google驱动器,csv,google-apps-script,google-sheets,Csv,Google Apps Script,Google Sheets,我正在使用脚本从链接导入csv,但有一种方法可以使其适应从ftp链接或google驱动器文件导入? 谢谢 这是我的剧本 function myFunction3() { // 1. Set the required columns as the column number. const requiredColumns = [1, 5, 20]; // Please set the required columns. These values are from your question.

我正在使用脚本从链接导入csv,但有一种方法可以使其适应从ftp链接或google驱动器文件导入? 谢谢 这是我的剧本

function myFunction3() {
  // 1. Set the required columns as the column number.
  const requiredColumns = [1, 5, 20]; // Please set the required columns. These values are from your question.

  // 2. Retrieve CSV data from an URL.  
  const url = "https://www.stanem.it/csv/InnovaCSV.csv";
  const res = UrlFetchApp.fetch(url);

  // 3. Convert CSV data to Spreadsheet.
  const id = Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS, title: "tempSpreadsheet"}, res.getBlob()).id;

  // 4. Delete the columns except for the required columns.
  const ss = SpreadsheetApp.openById(id);
  const sheet = ss.getSheets()[0];
  const maxColumn = sheet.getMaxColumns();
  const requests = [];  
  for (let i = 1; i <= maxColumn; i++) {
    if (!requiredColumns.includes(i)) {
      requests.push({deleteDimension: {range: {sheetId: sheet.getSheetId(), dimension: "COLUMNS", startIndex: i - 1, endIndex: i}}});
    }
  }
  Sheets.Spreadsheets.batchUpdate({requests: requests.reverse()}, id);

  // 5. Copy the values of modified CSV data to a sheet in the active Spreadsheet.
  const destinationSheetName = "Sheet1";  // Please set the destilnation sheet name in the active Spreadsheet.
  const dstss = SpreadsheetApp.getActiveSpreadsheet();
  const values = Sheets.Spreadsheets.Values.get(id, sheet.getSheetName()).values;
  Sheets.Spreadsheets.Values.update({values: values}, dstss.getId(), destinationSheetName, {valueInputOption: "USER_ENTERED"});
  
  // 6. Remove the temporat Spreadsheet.
  DriveApp.getFileById(id).setTrashed(true);
}
函数myFunction3(){
//1.将所需列设置为列号。
const requiredColumns=[1,5,20];//请设置所需的列。这些值来自您的问题。
//2.从URL检索CSV数据。
常量url=”https://www.stanem.it/csv/InnovaCSV.csv";
const res=UrlFetchApp.fetch(url);
//3.将CSV数据转换为电子表格。
const id=Drive.Files.insert({mimeType:mimeType.GOOGLE_SHEETS,title:“tempseadsheet”},res.getBlob()).id;
//4.删除除所需列之外的列。
const ss=SpreadsheetApp.openById(id);
const sheet=ss.getSheets()[0];
const maxColumn=sheet.getMaxColumns();
常量请求=[];
对于(假设i=1;i问题和解决方法:
不幸的是,在当前阶段,无法使用UrlFetchApp从FTP检索该文件。因此,在您的情况下,如何从Google Drive检索该文件?从Google Drive检索CSV文件时,修改的脚本如下所示

修改脚本: 请按如下方式修改您的脚本。在这个修改后的脚本中,它假定CSV文件放在您的Google驱动器中

发件人: 致:
  • 在这种情况下,可以使用
    Drive.Files.copy
    转换CSV文件
参考:
问题和解决方法: 不幸的是,在当前阶段,无法使用UrlFetchApp从FTP检索该文件。因此,在您的情况下,如何从Google Drive检索该文件?从Google Drive检索CSV文件时,修改的脚本如下所示

修改脚本: 请按如下方式修改您的脚本。在这个修改后的脚本中,它假定CSV文件放在您的Google驱动器中

发件人: 致:
  • 在这种情况下,可以使用
    Drive.Files.copy
    转换CSV文件
参考:
// 2. Retrieve CSV data from an URL.  
const url = "https://www.stanem.it/csv/InnovaCSV.csv";
const res = UrlFetchApp.fetch(url);

// 3. Convert CSV data to Spreadsheet.
const id = Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS, title: "tempSpreadsheet"}, res.getBlob()).id;
// 3. Convert CSV data to Spreadsheet.
const fileId = "### fileId of CSV file on Google Drive ###";  // Please set the file ID of CSV file.
const id = Drive.Files.copy({mimeType: MimeType.GOOGLE_SHEETS, title: "tempSpreadsheet"}, fileId).id;