Overwriting Spreadsheet to Existing Excel File

This sample script converts a spreadsheet to excel file, and overwrites the excel file to the existing excel file. When you use this script, at first, please confirm whether Drive API is enabled at Google API console. Because the existing excel file is overwritten, the file name and file ID are not changed.

function overWrite(src_spreadsheetId, dst_excelfileId) {
  var accesstoken = ScriptApp.getOAuthToken();
  return UrlFetchApp.fetch(
    "https://www.googleapis.com/upload/drive/v3/files/" +
    dst_excelfileId +
    "?uploadType=multipart",
    {
      method: "PATCH",
      headers: {Authorization: "Bearer " + accesstoken},
      contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
      payload: function(a, s) {
        return UrlFetchApp.fetch(
          "https://www.googleapis.com/drive/v3/files/" +
          s +
          "/export?mimeType=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
          {
            method: "GET",
            headers: {Authorization: "Bearer " + a},
            muteHttpExceptions: true
          }
        ).getBlob().getBytes();
      }(accesstoken, src_spreadsheetId)
    }
  ).getContentText();
}

 Share!