Google apps script google脚本只导入少数带有脚本的列
有一个CSV文件,其中包含了大量的列和原始然而,我只想导入一些列 我在下面的网页链接中使用了这个脚本。 它可以工作,但会导入包含所有列和行的完整文件。 我只需要导入少数列,而不是全部。 例:第1栏、第5栏、第20栏 有人能帮我吗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
我相信你的目标如下
- 您希望从URL检索CSV数据
- 您希望通过检索特定列将CSV数据放入Google电子表格
- 您希望使用谷歌应用程序脚本实现这一点。
- 当我看到
在你的问题中,我知道脚本是Google Apps脚本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/
- 在当前阶段,
可用于将CSV数据作为数组进行解析。使用此方法时,CSV数据可以解析为二维数组。我想这可能是可以使用的Utilities.parseCsv()
- 为了检索特定的列,我认为可以从CSV数据解析的数组中检索
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数据使用特定的分隔符,请修改
toconst ar=Utilities.parseCsv(res.getContentText())
const ar=Utilities.parseCsv(res.getContentText(),“delimiter”)代码>李>
- 当您希望将脚本作为自定义函数运行时,还可以使用以下脚本。在这种情况下,请将
放入单元格。这样,检索特定列的CSV数据就会被放入=SAMPLE(“URL”,“1,5,20”)
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()])); }
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);
}