Import csv data into google sheets using apps script

隐身守侯 提交于 2021-01-29 20:37:38

问题


I would like to import a csv file into Google Sheets using Apps Script. I would like to add the csv file to be read to the existing tab "csv_data" after the last line. Can someone help me? I am a starting apps script user. Thank you in advance for your help! GJM


回答1:


There is a useful method, called Utilities.parseCsv()

If your csv file is located on your Google drive, you can import and append it e.g. by attaching to your spreadsheet a bound script with the following content:

function appendCSV() {  
  var file = DriveApp.getFilesByName("PASTE HERE THE NAME OF YOUR CSV FILE INCLUDING EXTENSION").next();
  var csvData = Utilities.parseCsv(file.getBlob().getDataAsString(),'SPECIFY HERE THE DELIMITER (OPTIONAL)');
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow=sheet.getLastRow();
  sheet.getRange(lastRow, 1, csvData.length, csvData[0].length).setValues(csvData);  
}


来源:https://stackoverflow.com/questions/59281879/import-csv-data-into-google-sheets-using-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!