0

I want to automatically import an xls file that i save to a specified local folder on a laptop weekly to a Google Sheets.

I want to replace the data in a sheet each time the import is done. The file name changes everytime also but does always start with report so could I use "report*"

If anyone can help with the script it would be appreciated, it will save me a manual import each time

Thanks Martin

I have never used the Google Apps Script extension previously and I seem to be having issues trying to define the local drive as the location with the filename changing each time.

Example location would be C:\Reports\ and the filename will always start with Report (report123456.xls) as example, I want to then import that to the existing Sheet named Details

3
  • In your situation, I thought that the languages which can run with your local PC might be useful rather than Google Apps Script because Google Apps Script cannot directly access your local PC. But, if this was not your expected direction, I apologize. Commented Dec 6, 2022 at 13:11
  • thanks Tanaike, that makes sense in that Google Apps Script cannot access the local PC.. When you say Languages, are you referring to Python, Node.JS, VBA etc? Thanks Commented Dec 6, 2022 at 14:12
  • Thank you for replying. About When you say Languages, are you referring to Python, Node.JS, VBA etc?, it's yes. Commented Dec 6, 2022 at 23:03

1 Answer 1

1

You could upload a local XLS file using a pop-up prompt within your Google Sheet. Make sure you assign the script importExcel to a button or a menu option.

Here is a working script:

function importExcel(e) {

  if (!e) {
    SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutputFromFile("uploadExcelpopup"), "Upload PIMMS Excel file");
    return;
  }

  var file = Utilities.newBlob(...e);

  let config = {
    title: "[Temporary Google Sheet] " + file.getName(),
    mimeType: MimeType.GOOGLE_SHEETS
  };
  let tempSs = Drive.Files.insert(config, file);

  var 
  fileId = tempSs.id,
  source = SpreadsheetApp.openById(fileId),
  sourceSheet = source.getSheets()[0];

    SpreadsheetApp.getActive().toast('Extracting data','🔄 File uploaded',10);

  var
  sourceValues = sourceSheet.getRange(1,1,sourceSheet.getLastRow(),sourceSheet.getLastColumn()).getValues(),
  sheetDataset = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Dataset");

  sheetDataset.getRange(1, 1, sourceValues.length, sourceValues[0].length).setValues(sourceValues);

  DriveApp.getFileById(fileId).setTrashed(true);

    SpreadsheetApp.getActive().toast('', '✔️ Upload complete',10);
    
}

You will also need to create an HTML file:

<form>
  <input type="file" name="file" onchange="importExcel(this.parentNode)" accept=".xls,.xlsx,.xlsm" id="inputFile" >
  <font face = "Calibri" size = "3"> <br><div id="progress">⏳ Waiting</div></font>
</form>
<script>
function importExcel(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);
  }).importExcel([[...new Int8Array(d.target.result)], file.type, file.name]);
  f.readAsArrayBuffer(file);
  
}
</script>

This is based on Tanaike's CSV local input pop-up suggestion:

Google Sheets Import CSV popup

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.