title: Convert excel files to Google Spreadsheet and replace existing spreadsheet files automatically
body: <p>The code provided convert files from Excel to Google Sheet. <a href="https://stackoverflow.com/a/49265306/9337071">The code from here</a> but it does not overwrite/replace current existing spreadsheet files in the destination folder.
  Is it possible to convert everything including the ones inside the subfolders altogether and replace any existing Google Spreadsheet files with the same name?</p>
  
  <pre><code>function convertCollection1() 
  {
    var user = Session.getActiveUser(); // Used for ownership testing.1aJcbdGhwliTs_CZ-3ZUvQmGRDzBM7fv9
    var origin = DriveApp.getFolderById("1dPsDfoqMQLCokZK4RN0C0VRzaRATr9AN");
    var dest = DriveApp.getFolderById("1M6lDfc_xEkR4w61pUOG4P5AXmSGF1hGy");
  
    // Index the filenames of owned Google Sheets files as object keys (which are hashed).
    // This avoids needing to search and do multiple string comparisons.
    // It takes around 100-200 ms per iteration to advance the iterator, check if the file
    // should be cached, and insert the key-value pair. Depending on the magnitude of
    // the task, this may need to be done separately, and loaded from a storage device instead.
    // Note that there are quota limits on queries per second - 1000 per 100 sec:
    // If the sequence is too large and the loop too fast, Utilities.sleep() usage will be needed.
    var gsi = dest.getFilesByType(MimeType.GOOGLE_SHEETS), gsNames = {};
    while (gsi.hasNext())
    {
      var file = gsi.next();
      if(file.getOwner().getEmail() == user.getEmail())
        gsNames[file.getName()] = true;
  
      Logger.log(JSON.stringify(gsNames))
    }
  
    // Find and convert any unconverted .xls, .xlsx files in the given directories.
    var exceltypes = [MimeType.MICROSOFT_EXCEL, MimeType.MICROSOFT_EXCEL_LEGACY];
    for(var mt = 0; mt &lt; exceltypes.length; ++mt)
    {
      var efi = origin.getFilesByType(exceltypes[mt]);
      while (efi.hasNext())
      {
        var file = efi.next();
        // Perform conversions only for owned files that don't have owned gs equivalents.
        // If an excel file does not have gs file with the same name, gsNames[ ... ] will be undefined, and !undefined -&gt; true
        // If an excel file does have a gs file with the same name, gsNames[ ... ] will be true, and !true -&gt; false
        if(file.getOwner().getEmail() == user.getEmail() &amp;&amp; !gsNames[file.getName().replace(/\.[^/.]+$/, "")])
        {
          Drive.Files.insert (
            {title: file.getName(), parents: [{"id": dest.getId()}]},
            file.getBlob(),
            {convert: true}
          );
          // Do not convert any more spreadsheets with this same name.
          gsNames[file.getName()] = true;
        }
      }
    }
    Logger.log(JSON.stringify(gsNames))
  }
  </code></pre>