Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix the issue in deleting row without file name #90

Closed
chriskyfung opened this issue Dec 2, 2023 · 1 comment · Fixed by #87
Closed

🐛 Fix the issue in deleting row without file name #90

chriskyfung opened this issue Dec 2, 2023 · 1 comment · Fixed by #87
Assignees
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@chriskyfung
Copy link
Owner

chriskyfung commented Dec 2, 2023

Symptoms

The deleteSelected() function is failing to delete selected log items when DriveApp.getFileById(item.fileId) cannot find the file or item.fileId is empty. This is causing an error in the script and preventing the deletion of selected items and their files from Google Drive.

Instructions for bug fixing

  1. Check if the item.fileId is empty or null before calling DriveApp.getFileById(item.fileId).
  2. If item.fileId is empty or null, skip the file deletion step and move on to the next item.
  3. If DriveApp.getFileById(item.fileId) cannot find the file, log an error message and move on to the next item.
  4. Update the code as follows:
/**
 * Delete selected log entries from the log sheet and
 * their files from Google Drive
 */
export function deleteSelected() {
  const logsSheet = SpreadsheetApp.getActive().getSheetByName(
    sheetNames['logs']
  );
  const items = getSelected().items;
  const msg = Browser.msgBox(
    'Delete Seleted Items',
    `Are you sure you want to delete these ${items.length} items and their files from your Drive?`,
    Browser.Buttons.YES_NO
  );
  if (msg === 'yes') {
    items.forEach((item, index) => {
      if (item.fileId) {
        const file = DriveApp.getFileById(item.fileId);
        if (file) {
          file.setTrashed(true);
        } else {
          console.error(`File not found for item ${item.row}`);
        }
      }
      logsSheet.deleteRow(item.row - index);
    });
  }
}

Citations:
[1] https://developers.google.com/apps-script/guides/support/troubleshooting
[2] https://stackoverflow.com/questions/37924842/deleting-rows-in-google-sheets-using-google-apps-script
[3] https://support.google.com/docs/thread/101625681/how-to-automate-deletion-of-old-form-responses-from-the-form?hl=en
[4] https://groups.google.com/g/google-apps-script-community/c/SHAYPU8Cjug
[5] https://community.airtable.com/t5/automations/unable-to-delete-10-records-at-a-time-can-anyone-solve-this/td-p/156592

@chriskyfung chriskyfung self-assigned this Dec 2, 2023
@chriskyfung chriskyfung added the bug Something isn't working label Dec 2, 2023
@chriskyfung chriskyfung added the good first issue Good for newcomers label Dec 27, 2023
@chriskyfung
Copy link
Owner Author

How can you handle errors when getfilebyid cannot find the file or item.fielid is empty

To handle errors when DriveApp.getFileById(item.fileId) cannot find the file or item.fileId is empty in Google Apps Script, you can use a try-catch block to catch the error and provide appropriate error handling. Here's an example of how to handle the error:

try {
  const file = DriveApp.getFileById(item.fileId);
  if (file) {
    // File found, proceed with the deletion
    file.setTrashed(true);
  } else {
    // File not found
    console.error('File not found for item ' + item.row);
  }
} catch (e) {
  // Handle the error
  console.error('An error occurred: ' + e);
  // You can also set a default value for the file name, for example:
  // name = "No access";
}

In this example, the try block attempts to get the file by its ID. If the file is found, it proceeds with the deletion. If the file is not found, it logs an error. If an error occurs, it is caught in the catch block, and appropriate error handling is performed, such as logging the error message.

This approach allows you to gracefully handle the scenarios where the file is not found or the item.fileId is empty, ensuring that the script does not fail unexpectedly.

Citations:
[1] https://www.reddit.com/r/GoogleAppsScript/comments/hzo6pc/check_if_driveappgetfilebyid_is_false/?rdt=40425
[2] https://rollbar.com/blog/csharp-filenotfoundexception/
[3] https://stackoverflow.com/questions/64764823/driveapp-getfilebyidfileid-error-no-item-with-the-given-id-could-be-found
[4] https://developers.google.com/apps-script/reference/drive/drive-app
[5] https://developers.google.com/apps-script/reference/drive/file

chriskyfung added a commit that referenced this issue Jan 1, 2024
Changes in `deleteSelected()`:
- Add a condition to check if `item.fileId` is null or empty
- Avoid calling DriveApp to delete file if item.fileId is not available
- Log a warning message to system log if `item.fileId` is not available

Changes in `getSelected()`:
- Use early exit to skip the current iteration in the for loop if
  `isChecked` is false
- Retrieve row data from the spreadsheet and include them in the `items`
  object

Fixes issue #90
@chriskyfung chriskyfung mentioned this issue Jan 1, 2024
16 tasks
@chriskyfung chriskyfung linked a pull request Jan 1, 2024 that will close this issue
16 tasks
@chriskyfung chriskyfung pinned this issue Jan 1, 2024
@chriskyfung chriskyfung changed the title 🐛 Skip non-existing files when deleting selected log entries 🐛 Fix the issue in deleting row without file name Feb 5, 2024
@chriskyfung chriskyfung unpinned this issue Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
1 participant