Home > OS >  How to call an API running locally from Apps Script
How to call an API running locally from Apps Script

Time:08-02

  • I am building a Container Bound Script using AppsScript wrt Google Sheets.
  • I am taking input by adding a menu on the google sheet as shown below,

enter image description here

  • And when clicked, prompted with a HTML form, in which we can upload a PDF, which is saved on Google Drive and the information regarding the file upload in saved on the google sheets, but I also want to send to a FastAPI running locally on my machine.

enter image description here

  • File Information stored on Google Sheets

enter image description here

  • Code for saving on Google Drive
function uploadFilesToGoogleDrive(data,name,type){                          
  var datafile = Utilities.base64Decode(data)    

  //create a new blob with decode data, name, type                           
  var blob2 = Utilities.newBlob(datafile, type, name);                      
  var folder = DriveApp.getFolderById("<url>"); 

  //Create new file (property of final user)
  var newFile = folder.createFile(blob2);                                   
  
   var rowData = [                                                          
    newFile.getName(),
    newFile.getUrl(),
    newFile.getDateCreated()
  ];
  SpreadsheetApp.getActive().getSheetByName("sheet1").appendRow(rowData);     
  
  return newFile.getUrl()                                                   
}

CodePudding user response:

It's not possible to run a Google Apps Scripts project locally, Google Apps Scripts projects run on Google Servers.

You can find more information here.

In your case, the only viable way I can think of for you to can call an API being served locally, would be to expose the appropriate endpoints from your API to the web. That way, the Google Servers responsible to run your script will have access to your API.

I understand that perhaps this solution is against the purpose of having an API server locally (since it won't be locally anymore), however Google Apps Script is designed to work on the Cloud.

In any case, you may find the Apps Script Class UrlFetchApp useful for making HTTP requests for your own API of any other.

  • Related