Importing a local CSV file into Google Sheets using Apps Script

1.4k Views Asked by At

I'm creating shipping labels and pack some of my friends’ online orders. I tried to create a shared google spreadsheet for them to easily send me the address information. I recently added a checkbox to import a predefined CSV file with an onEdit() trigger, so that they could share the information easier than before. They sell on some ecommerce platforms so that they can export their order details as CSV and upload it into my sheet easily. Instead of a button, I use a checkbox cell value to trigger the onEdit function so that the script can edit cells even if the user triggering the function doesn't have edit permission on those cells. I'm not good at html concepts and I found an import from local computer script which writes the CSV into the sheet I specified.

(Edit: The code I used is from the answer of this question as pointed out in the comment. It was the starting point for me to think something like this. My issue persists since this code is not working on other computers who can access with edit permissions to my sheet. My test user can do the same thing to trigger a task on several computers, only able to complete the task on my computer but not the other 3 currently tried. All have Windows 10 OS, and Chrome as browser. Didn't asked for specific permissions, no error log.

Below is the remaining part of the main thread)

I created the script file on my home computer with account A as owner, then add account B as a test account with edit permissions. There are protected cells and sheets editable for "only me" which is account A, and I tried writing the CSV contents into one of those protected sheets. Any user will have the same access with test account B, so I did my test using both account A and B. The script worked (and still works) as expected both with account A and B on my computer, i.e. when I clicked the checkbox it opened a dialog for me to choose a CSV, then parse it and add it to the desired sheet.

When I share the sheet with a real user, he can't passed the uploading process. When I checked the executions page, I see that it didn’t give me any errors but “completed” for all the onEdit entries. To understand if the issue is CSV related, I shared the CSV file I used for my user to test and get his CSV file to test it myself. I can upload his file from my computer without an issue, and he couldn’t upload mine which means the script doesn't work on his computer.

I logged in my test account from another computer I have and tried to trigger this script. I mean checked the box to see if it runs smoothly, I didn't passed the uploading process as well. So there is something working on my computer that other computers don't have. I don't know if it's related to the uploading script or some permissions that I gave before, but I don't recall giving any extra permissions for local file access, or if it’s really a thing. Plus, upload file button allows anyone who tried to choose a local file on the computer, so no access issue but somehow, the script can’t process the file and upload it.

I found a similar question here https://stackoverflow.com/questions/11273268/script-import-local-csv-in-google-spreadsheet which was the first thing I tried, but as the comment below it says, I got an error saying UiApp function has been deprecated, so it didn't work.

Thanks for any help!

Here are the script and html file that I used. Both the function and html file are named "importCSV"

function importCSV(e){
  
  const MasterCsvFileSheet = SpreadsheetApp.openById("CsvSpreadsheetID").getSheetByName("ImportHereSheet");    
  
  if (!e) {
    SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutputFromFile("importCSV"), "Choose CSV");
    return;
  }
  const csv = Utilities.parseCsv(Utilities.newBlob(...e).getDataAsString());
  
  MasterCsvFileSheet.getRange(MasterCsvFileSheet.getLastRow() + 1, 1, csv.length, csv[0].length).setValues(csv);

  /*
   ****Some other code for calculations follows after this line****
  */
  
}
<form>
  <input type="file" name="file" onchange="importCSV(this.parentNode)" accept=".csv,text/csv">
  <div id="progress">Waiting</div>
</form>
<script>
function importCSV(e) {
  const div = document.getElementById("progress");
  div.innerHTML = "Uploading...";
  const file = e.file.files[0];
  const f = new FileReader();
  f.onload = d => google.script.run.withSuccessHandler(_ => {
    div.innerHTML = "Done";
    setTimeout(google.script.host.close, 1000);
  }).importCSV([[...new Int8Array(d.target.result)], file.type, file.name]);
  f.readAsArrayBuffer(file);
}
</script>
0

There are 0 best solutions below