Uploading a PDF file to Google Drive and renaming it according to Appsheet Logic

Hello Community,

I would be grateful to get your help to rename my PDF-s I upload to an Appsheet and download them to a certain folder in Google Drive. I failed to rename it to make the File Name clean, without an Appsheet generated name.
Any hint will be of great help

Looking forward to getting your prompt 

@MultiTech 

Solved Solved
0 2 152
1 ACCEPTED SOLUTION

Yeah we don't have control over the filenames when uploading a file like that - but you can use a script to change them after the fact if you want.

I worked with Appster to provide the following answer, which makes use of a library script I've got shared publicly that will find the file in your google drive - from there you can then do whatever you want with it, such as rename it (or even move it to another folder).

--------------------------------------------------------------------------------------------------------

### Overview of the Solution

1. **Leverage the Shared Library Script:** Use the `findFileInPath` function from the provided library to locate the uploaded file in Google Drive based on the path specified in your AppSheet.

2. **Rename the File:** Once the file is located, rename it based on your desired naming convention.

3. **Move (Optional):** Move the file to a specific folder if needed, using the Google Drive APIs.

### The Script

Below is a script that integrates the `findFileInPath` function and handles the renaming and moving of the file:

function renameAndMoveFile(filePath, newFileName, targetFolderId, startingFolderId, logging) {
// Locate the file using the findFileInPath library function
let file = findFileInPath(startingFolderId, filePath, logging, 'file');

if (file) {
// Rename the file
file.setName(newFileName);

// Move the file to the target folder (if specified)
if (targetFolderId) {
let targetFolder = DriveApp.getFolderById(targetFolderId);
targetFolder.addFile(file);

// Remove file from its current parent folder to avoid duplicates
let originalFolder = file.getParents().next();
originalFolder.removeFile(file);
}

return 'File renamed and moved successfully';
} else {
return 'File not found';
}
}

// Helper function to extract file ID from URL
function getFileIdFromUrl(url) {
return url.match(/[-\w]{25,}/);
}

### Instructions for Adding the Library Script

1. **Open Your Script Editor**:
- Go to your Google Sheet.
- Select `Extensions` > `Apps Script`.

2. **Add the Library**:
- In the Apps Script Editor, click on `Libraries` from the left sidebar.
- Enter the Project key: `1CjNsx44zvMjSs1OGumGGjml0qCDUqmx6w_FjuKHUL4FQ478G9ml1t-s2`.
- Click `Add`, name it appropriately, and ensure it's included in your script.

3. **Set Up Triggers**:
- Set a trigger in AppSheet automation to run the script whenever a new file is uploaded. You can do this by watching updates in your connected Google Sheet and executing the `renameAndMoveFile` script accordingly.
- Ensure logging is set to `true` during initial runs to troubleshoot any issues, and then disable it later for routine operations.

By following these steps and using the provided script, you'll efficiently rename and manage your uploaded PDFs in Google Drive, integrating smoothly with your AppSheet application. If you encounter any issues or have further questions, feel free to reach out to the [AppSheet Insider community](https://community.appsheet-insider.com/) or the [Answer Portal](https://www.multitechvisions.com/answer-portal) for more tailored assistance!

View solution in original post

2 REPLIES 2

Yeah we don't have control over the filenames when uploading a file like that - but you can use a script to change them after the fact if you want.

I worked with Appster to provide the following answer, which makes use of a library script I've got shared publicly that will find the file in your google drive - from there you can then do whatever you want with it, such as rename it (or even move it to another folder).

--------------------------------------------------------------------------------------------------------

### Overview of the Solution

1. **Leverage the Shared Library Script:** Use the `findFileInPath` function from the provided library to locate the uploaded file in Google Drive based on the path specified in your AppSheet.

2. **Rename the File:** Once the file is located, rename it based on your desired naming convention.

3. **Move (Optional):** Move the file to a specific folder if needed, using the Google Drive APIs.

### The Script

Below is a script that integrates the `findFileInPath` function and handles the renaming and moving of the file:

function renameAndMoveFile(filePath, newFileName, targetFolderId, startingFolderId, logging) {
// Locate the file using the findFileInPath library function
let file = findFileInPath(startingFolderId, filePath, logging, 'file');

if (file) {
// Rename the file
file.setName(newFileName);

// Move the file to the target folder (if specified)
if (targetFolderId) {
let targetFolder = DriveApp.getFolderById(targetFolderId);
targetFolder.addFile(file);

// Remove file from its current parent folder to avoid duplicates
let originalFolder = file.getParents().next();
originalFolder.removeFile(file);
}

return 'File renamed and moved successfully';
} else {
return 'File not found';
}
}

// Helper function to extract file ID from URL
function getFileIdFromUrl(url) {
return url.match(/[-\w]{25,}/);
}

### Instructions for Adding the Library Script

1. **Open Your Script Editor**:
- Go to your Google Sheet.
- Select `Extensions` > `Apps Script`.

2. **Add the Library**:
- In the Apps Script Editor, click on `Libraries` from the left sidebar.
- Enter the Project key: `1CjNsx44zvMjSs1OGumGGjml0qCDUqmx6w_FjuKHUL4FQ478G9ml1t-s2`.
- Click `Add`, name it appropriately, and ensure it's included in your script.

3. **Set Up Triggers**:
- Set a trigger in AppSheet automation to run the script whenever a new file is uploaded. You can do this by watching updates in your connected Google Sheet and executing the `renameAndMoveFile` script accordingly.
- Ensure logging is set to `true` during initial runs to troubleshoot any issues, and then disable it later for routine operations.

By following these steps and using the provided script, you'll efficiently rename and manage your uploaded PDFs in Google Drive, integrating smoothly with your AppSheet application. If you encounter any issues or have further questions, feel free to reach out to the [AppSheet Insider community](https://community.appsheet-insider.com/) or the [Answer Portal](https://www.multitechvisions.com/answer-portal) for more tailored assistance!

Thanks a lor for your time and attention. Unfortunately I am not a code/apple script adept to start with so huge task as any task reuiqres expertise... I have managed to solve the issue with Zapier. 
I am fan of of MultitechVisions, hope we will have more chances to work and crack huge things in the future ))
Good Luck!