error using Goggle apps script in a automation

Hello!

I have a code in Google Apps script that I am using in an automation in my app. It is not working, I want to obtain the link of my pdf file, but the column is empty when I trigger the automation.

How can I use a kind of "log message" , I need to see what happens with the script in appsheet, why the link is not written in my column?

Thank you for your help!

p.s. in google apps script in Execution, there is errors: if I activate "return values" - TypeError: Cannot read properties of undefined (reading 'parameter') at doGet(Code:2:20), if I  activate "return values" but without give something in "Specific type" - "Name", this error : "TypeError: Cannot read properties of undefined (reading 'fileName') at doGet(Code:2:30)".

Many thanks!

Regards!

Ruxandra

0 14 2,128
14 REPLIES 14

Steve
Platinum 5
Platinum 5

The values for columns of type File and Image are written by the server after the associated file has been uploaded. This upload typically takes a few seconds, so if your script triggers immediately upon the row arriving at the server, it's likely the file upload has not completed yet and so there is no file specification recorded.

Hello!

Yes, google script is the next step in my automation, after the creation of the pdf file. How could I delay the script's execution? I know there is a step "Delay", but the minimum time is 5 minutes which is too much.

Thank you!

Ruxandra

set

 Logger.log('Response Code: ' + filename);

and check is script executions.

Can you share your code?

Hello!

This is the code:

function doGet(e) {

  var fileName = e.parameter.fileName; // Get the file name from the AppSheet request

  //var driveId = e.parameter.driveId; // Get the shared drive ID from the AppSheet request

 

 // fileName = "example.pdf"

  driveId = "mydriveID"

 

  try {

    // Use the advanced Drive service to search for the file

    var files = Drive.Files.list({

      q: "name = '" + fileName + "' and trashed = false and '" + driveId + "' in parents",

      supportsAllDrives: true,

      includeItemsFromAllDrives: true,

      fields: "files(id, name, webViewLink, parents)"

    });

   

    if (files.files && files.files.length > 0) {

      var file = files.files[0];

      var fileId = file.id;

      var filePath = file.webViewLink;

     

      // Get the parent folder name

      var folderName = "Root";

      if (file.parents && file.parents.length > 0) {

        var folder = Drive.Files.get(file.parents[0], {

          supportsAllDrives: true,

          fields: "name"

        });

        folderName = folder.name;

      }

 

      Logger.log(filePath)

     

      return ContentService.createTextOutput(JSON.stringify({

        id: fileId,

        name: fileName,

        path: filePath,

        folder: folderName

      })).setMimeType(ContentService.MimeType.JSON);

    } else {

      throw new Error("File not found");

    }

  } catch (error) {

    Logger.log(error)

    return ContentService.createTextOutput(JSON.stringify({

      error: "Error: " + error.toString()

    })).setMimeType(ContentService.MimeType.JSON);

  }

}

 

Thank you!

Your script request is like:
https://script.google.com/macros/s/your_script_id/exec?fileName=example.pdf

Are you running code on dry?

jambyc_0-1733388505015.png

jambyc_1-1733388542749.png

 

Hello!

One of my colleague solved the issue using python in/and bigquery. Thank you all for your help!.

Regards!

Ruxandra

That is not a proper appscript file to do the task you want.  That function is part of a webapp and cannot be called using an appscript task.

Hello!

One of my colleagues solved the issue using python in/and BigQquery. Thank you all for your help!

Regards!

Ruxandra

How do you create pdf file? From appsheet?
Once pdf file is created (and Saved by appsheet) you can call it from the app.


Your e.parameter.fileName- Is it included in your URL script ?

..../your_script_id/exec?fileName=example.pdf

Steve
Platinum 5
Platinum 5

@Ruxandra wrote:

How could I delay the script's execution?


sleep()

Hello!

One of my colleague solved the issue using python in/and bigquery. Thank you all for your help!.

Regards!

Ruxandra

IF you just need a script to retrieve a file that has already been created in a Folder in your drive and return the link. This script will take the file name passed from appsheets and the File ID (This can be made static in the script if needed). The function uses sleep and waits 60 seconds. Then goes to the file and subfolders inside it and either matches the file name or based on a partial match as well if you use keys for the name. Then takes the file ID and creates the File URL and returns as a text string to appsheets to be pasted in your row cell of choice. Also lots of loggers to diagnose if needed. I recommend removing the ones that state the file id and folder id when you move to full roll out  ๐Ÿ˜

function getFileId(fileName, folderId) {
  Logger.log("Starting getFileId function...");

  // Pause for 60 seconds (60000 milliseconds)
  Logger.log("Pausing execution for 60 seconds...");
  Utilities.sleep(60000);

  Logger.log("File name (exact or partial match): " + fileName);
  Logger.log("Folder ID: " + folderId);

  try {
    // Get the folder by ID
    var folder = DriveApp.getFolderById(folderId);
    if (!folder) {
      throw new Error("Folder not found with ID: " + folderId);
    }
    Logger.log("Folder found: " + folder.getName());

    // Check for the file in the main folder
    Logger.log("Checking in the main folder...");
    var fileLink = searchFolderForFile(fileName, folder);
    if (fileLink) {
      Logger.log("File found in main folder. Returning link: " + fileLink);
      return ContentService.createTextOutput(fileLink).setMimeType(ContentService.MimeType.TEXT);
    }

    // If not found, recursively search subfolders
    Logger.log("File not found in the main folder. Checking subfolders...");
    var subfolders = folder.getFolders();
    while (subfolders.hasNext()) {
      var subfolder = subfolders.next();
      Logger.log("Checking subfolder: " + subfolder.getName());
      fileLink = searchFolderForFile(fileName, subfolder);
      if (fileLink) {
        Logger.log("File found in subfolder. Returning link: " + fileLink);
        return ContentService.createTextOutput(fileLink).setMimeType(ContentService.MimeType.TEXT);
      }
    }

    throw new Error("File not found with a matching name: " + fileName);
  } catch (error) {
    Logger.log("Error occurred: " + error.toString());
    // Return the error as plain text
    return ContentService.createTextOutput("Error: " + error.toString()).setMimeType(ContentService.MimeType.TEXT);
  }
}

// Helper function to search for the file in a given folder
function searchFolderForFile(fileName, folder) {
  var files = folder.getFiles();
  while (files.hasNext()) {
    var file = files.next();
    if (file.getName().includes(fileName)) { // Partial or exact match
      var fileId = file.getId();
      var viewableLink = "https://drive.google.com/file/d/" + fileId + "/view";
      Logger.log("File match found. Viewable link: " + viewableLink);
      return viewableLink;
    }
  }
  return null; // File not found in this folder
}

Hello!

One of my colleagues solved the issue using python in/and BigQuery. Thank you all for your help!

Regards!

Ruxandra