我相信你的目标如下。
- 您想从 URL 检索 CSV 数据。
- 您希望通过检索特定列将 CSV 数据放入 Google 电子表格。
- 您希望使用 Google Apps 脚本实现此目的。
- 当我在您的问题中看到
https://bionicteaching.com/importing-csv-into-google-sheets-via-google-script/comment-page-1/ 的URL 时,我了解到该脚本是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());修改为const ar = Utilities.parseCsv(res.getContentText(), "delimiter");。 Ref
注意:
-
当您想将脚本作为自定义函数运行时,您还可以使用以下脚本。在这种情况下,请将=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 数据时,发现它有 4,763,515 个单元格,42,155 行和 113 列。所以,为了解决这个问题,我想提出如下第二个示例脚本。
在此示例中,首先使用 Drive API 将 CSV 数据转换为电子表格,并使用 Sheets API 删除除所需列之外的列,然后将工作表复制到活动电子表格中。
示例脚本:
在使用此脚本之前,please enable Drive API and Sheets API at Advanced Google services。由于数据量大,我使用了 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);
}
新增2:
对不起这张sheet.copyTo(dstss);有效,但它给我创造了很多复印表,我只需要一张总是同名的表
根据您的回复,我为此修改了上面的脚本。
示例脚本:
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);
}
- 此示例脚本将修改后的 CSV 数据放到活动电子表格的特定工作表中。
- 在这种情况下,值从第一行第一列开始。所以当你想放其他范围时,请修改脚本。