[英]google script import only few column with script
有一个 CSV 文件,其中包含很多列和原始文件但是,我只想导入一些列,我在 web 上的链接中使用了这个脚本。 它可以工作,但它会导入包含所有列和行的完整文件。 我只需要导入几列而不是全部。 例如:第 1 列、第 5 列、第 20 列有人可以帮助我吗?
https://bionicteaching.com/importing-csv-into-google-sheets-via-google-script/comment-page-1/
我相信你的目标如下。
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 数据可以解析为二维数组。 我认为这也许可以使用。当以上几点反映到脚本中时,它变成如下。
请将以下脚本复制并粘贴到 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);
}
const ar = Utilities.parseCsv(res.getContentText());
到const ar = Utilities.parseCsv(res.getContentText(), "delimiter");
. 参考 当您想将脚本作为自定义 function 运行时,您还可以使用以下脚本。 在这种情况下,请将=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()])); }
从您提供的示例 CSV 数据中,我可以了解问题的原因。 我认为在这种情况下,对于上述方法,CSV 数据的大小可能会很大。 这样,我认为可能会发生这样的错误。 当我查看 CSV 数据时,发现它有 4,763,515 个单元格,42,155 行和 113 列。 因此,为了消除这个问题,我想提出如下第二个示例脚本。
在此示例中,首先使用 Drive API 将 CSV 数据转换为电子表格,然后使用表格 API 删除除所需列之外的列,然后将其复制到电子表格。
在使用此脚本之前, 请在 Google 高级服务中启用 Drive API 和 Sheets 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);
}
对不起这张表。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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.