Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script google脚本只导入少数带有脚本的列_Google Apps Script_Google Sheets_Import_Multiple Columns - Fatal编程技术网

Google apps script google脚本只导入少数带有脚本的列

Google apps script google脚本只导入少数带有脚本的列,google-apps-script,google-sheets,import,multiple-columns,Google Apps Script,Google Sheets,Import,Multiple Columns,有一个CSV文件,其中包含了大量的列和原始然而,我只想导入一些列 我在下面的网页链接中使用了这个脚本。 它可以工作,但会导入包含所有列和行的完整文件。 我只需要导入少数列,而不是全部。 例:第1栏、第5栏、第20栏 有人能帮我吗 我相信你的目标如下 您希望从URL检索CSV数据 您希望通过检索特定列将CSV数据放入Google电子表格 您希望使用谷歌应用程序脚本实现这一点。 当我看到https://bionicteaching.com/importing-csv-into-google-sh

有一个CSV文件,其中包含了大量的列和原始然而,我只想导入一些列 我在下面的网页链接中使用了这个脚本。 它可以工作,但会导入包含所有列和行的完整文件。 我只需要导入少数列,而不是全部。 例:第1栏、第5栏、第20栏 有人能帮我吗


我相信你的目标如下

  • 您希望从URL检索CSV数据
  • 您希望通过检索特定列将CSV数据放入Google电子表格
  • 您希望使用谷歌应用程序脚本实现这一点。
    • 当我看到
      https://bionicteaching.com/importing-csv-into-google-sheets-via-google-script/comment-page-1/
      在你的问题中,我知道脚本是Google Apps脚本
  • 您正在使用
    https://bionicteaching.com/importing-csv-into-google-sheets-via-google-script/comment-page-1/
修改点:
  • 在当前阶段,
    Utilities.parseCsv()
    可用于将CSV数据作为数组进行解析。使用此方法时,CSV数据可以解析为二维数组。我想这可能是可以使用的
  • 为了检索特定的列,我认为可以从CSV数据解析的数组中检索
当上述各点反映到脚本中时,它将变成如下所示

示例脚本: 请将以下脚本复制并粘贴到Google电子表格的脚本编辑器中。请设置变量,然后运行
myFunction
。这样,检索特定列的CSV数据将被放入活动工作表中

function myFunction() {
  // 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 = '###'; // Please set the direct link of CSV data.
  const res = UrlFetchApp.fetch(url);

  // 3. Parse CSV data.
  const ar = Utilities.parseCsv(res.getContentText());

  // 4. Retrieve the required columns from the CSV data.
  const values = ar.map(r => requiredColumns.map(i => r[i]));

  // 5. Put the values to the active sheet.
  const sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}
  • 如果您的CSV数据使用特定的分隔符,请修改
    const ar=Utilities.parseCsv(res.getContentText())
    to
    const ar=Utilities.parseCsv(res.getContentText(),“delimiter”)
    
注:
  • 当您希望将脚本作为自定义函数运行时,还可以使用以下脚本。在这种情况下,请将
    =SAMPLE(“URL”,“1,5,20”)
    放入单元格。这样,检索特定列的CSV数据就会被放入

      function SAMPLE(url, columns) {
        const requiredColumns = columns.split(",");
        const res = UrlFetchApp.fetch(url);
        return Utilities.parseCsv(res.getContentText()).map(r => requiredColumns.map(i => r[i.trim()]));
      }
    
参考资料:
增加1: 从您提供的示例CSV数据中,我可以了解问题的原因。我认为在这种情况下,对于上述方法,CSV数据的大小可能会很大。因此,我认为这样的错误可能会发生。当我检查CSV数据时,发现它有4763515个单元格,包含42155行和113列。因此,为了消除这个问题,我想提出如下第二个示例脚本

在此示例中,首先,使用驱动API将CSV数据转换为电子表格,并使用图纸API删除除所需列之外的列,然后将图纸复制到活动电子表格

示例脚本: 在使用此脚本之前。由于数据量大,我使用了Drive API和Sheets API

function myFunction2() {
  // 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";  // This is from your sample CSV data.
  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 sheet including CSV data to the active Spreadsheet.
  const dstss = SpreadsheetApp.getActiveSpreadsheet();
  sheet.copyTo(dstss).setName("sheetIncludingCSV");
  
  // 6. Remove the temporat Spreadsheet.
  DriveApp.getFileById(id).setTrashed(true);
}
函数myFunction2(){
//1.将所需列设置为列号。
const requiredColumns=[1,5,20];//请设置所需的列。这些值来自您的问题。
//2.从URL检索CSV数据。
常量url=”https://www.stanem.it/csv/InnovaCSV.csv“;//这是您的示例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();
常量请求=[];

对于(let i=1;i Hello,谢谢你的回复。我尝试了你的解决方案,但它给了我这个错误错误例外:无法解析文本。myFunction@Codice.gs:10我尝试阅读你的所有参考资料,但我找不到原因。你能帮我吗?谢谢”s@Stefano谢谢您的回复。给您带来的不便,我深表歉意。既然如此,我认为这样做是合理的要正确确认您的问题,需要确认您的CSV数据。因此,您可以提供复制您的问题的CSV数据样本吗?根据此信息,我想确认您的问题并考虑解决方案。如果您能够合作解决您的问题,我很高兴。您可以在以下链接找到我的CSV:谢谢您的支持support@Stefano Linguari感谢您的回复。根据您的示例CSV数据,我在我的回答中又添加了一个示例脚本。您能确认吗?如果这不是您期望的方向,我再次道歉。非常好。谢谢,谢谢,谢谢。我现在唯一的一个小问题是,此函数创建了一个工作表调用CSV,当我第二次启动函数时,给我这个错误:已经存在一个名为“sheetIncludingCSV”的工作表。有一种方法可以覆盖相同的工作表,而不必每次都创建新的工作表?再次感谢您的支持
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);
}