Error Handling in Google Apps Script UrlFetchApp

I am currently developing a customized timesheet using Google Docs and Google Apps Script. A key requirement is to convert the timesheet into a PDF format when it is submitted by the user. Below is the code snippet that I have implemented:

function createPdf(){

  var ss = SpreadsheetApp.getActiveSpreadsheet();

  var oauthConfig = UrlFetchApp.addOAuthService("google");
  oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
  oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope=https://spreadsheets.google.com/feeds/");
  oauthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
  oauthConfig.setConsumerKey("anonymous");
  oauthConfig.setConsumerSecret("anonymous");

  var url =  "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key="
      + ss.getId() +  "&gid=0&portrait=true" +"&exportFormat=pdf";

  var requestData = {
    "oAuthServiceName": "google",
    "oAuthUseToken": "always"
  };

  var result = UrlFetchApp.fetch(url, requestData);
  var content = result.getBlob();
  var file = DocsList.createFile(content);

  return file;

}

During debugging of the script, an error message appears as follows:

Unexpected exception upon serializing continuation

I would greatly appreciate any assistance with resolving this issue.

Answer №1

Upon conducting additional research, I came across the following resolution:

function generatePDF(){

  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var pdfFile = spreadsheet.getAs("application/pdf");
  var newFile = DocsList.createFile(pdfFile);
  newFile.rename("Test");

  return newFile;

}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using `reduce` in TypeScript, you can organize an array of objects based on a shared property

Here is an example of an array: [ { id: '1', task: 'Grocery shopping', isImportant: true }, { id: '2', task: 'Meeting', isImportant: false }, { id: '3', task: &apos ...

Displaying jQuery UI datepicker when an asp:LinkButton is clicked

How can we display the Jquery UI datepicker when clicking an asp.net linkbutton? This is the code snippet: <asp:LinkButton ID="PublishedSortLinkButton" runat="server" cssclass="datepicker" Text="<%$ Resources:Vacancies, DateRang ...

Vuejs may encounter challenges with v-model when numerous items are present on the page

My simple page consists of a form at the top for submitting data and a list below that, as shown in the image: https://i.sstatic.net/3WjY2.png The list is populated with data from an api, each object having only 4 properties. Currently, there are a total ...

Enhance the output object by including additional properties and then transform it into an array of objects

Here is the data I am sending to the backend system: { "appId":'10001', "upstream":[ { "name":"john", "content":"APPLICATION 1" }, { ...

Global regex replace is failing to replace the initial occurrence

In my attempt to create a simple template test, I encountered an issue with regular expressions. Despite using the \g modifier, only the last occurrence is being replaced instead of all copies. (How to String.match() distinct it ${SOME_TEXT} using Re ...

Can you make an element invisible with Jquery slide?

My goal is to achieve a similar effect as follows: $("#left").hide('slide', {direction: 'right'}, 1000) However, I do not want the div to be hidden entirely. Rather, I want it to maintain its space on the page by using visibility hidd ...

The URL for the Ajax request is unexpectedly filled with strange data

My current request looks like this: $.get("getdataforcharts", {q: ["test"]}, function (response) { alert( "success" ); }).done(function() { alert( "second success" ); }); I am expecting the URL to be: /testpage/getdataforcharts?q=t ...

executing a series of jQuery ajax calls iteratively

In my Web Application, I have implemented "Spatial Selection" on a map that allows users to select multiple streets. Once selected, each street is assigned a unique Street Id through the use of a "Selection Handler". The next step in the process involves ...

Animating the height of a div using jQuery after loading dynamic content

When I load a page using AJAX, the content is represented as a string. I am then trying to animate the height of a container if the content is larger than the current height of the container. However, for some reason, the variable _height always ends up ...

Difficulty executing for loop due to multiple buttons, resulting in malfunctioning buttons in JavaScript code

Currently encountering an issue while trying to implement modal windows on my first website. Everything works fine without the for loop, but I wanted to have multiple buttons and windows, so I decided to use a for loop to handle each button. Strangely, the ...

Having trouble importing a standalone directive into a standalone component

I am currently working on a directive that can capture the coordinates of the parent element and change the position of the child element based on the parent's x and y positions. Both components are standalone, and the directive has already been impo ...

Challenges when testing Angular controllers using Jasmine - modular problem

Recently, I made the decision to explore testing my Angular code using Jasmine. While everything seems to work fine without specific dependencies, I encountered problems when there are dependencies involved. For instance, in our project we use controllers. ...

Designing a Unique Shader with Three.js

Please be aware that a lot of the code has changed as per edit 3 below. I came across a blog post by Brandon Jones (link here) that I really liked. I wanted to convert his code to Three.js, but I'm encountering some difficulties. You can access his c ...

Progress bar for Ajax loading with an extensive list

Is there a way to create a progress bar that shows the real-time status of loading a large JSON list using an AJAX call? For example, displaying a message like "1 out of 200 loaded." Currently, my AJAX call is quite simple: function SendAjax(urlMethod, j ...

Tips for locating an entire row sequence within a 2D array?

What is the best way to count the number of rows in a matrix that do not contain a zero value? The code snippet provided only counts the number of columns, but I require the number of rows: var items = [ [1, 0, 1], [3, 3, 3], [5, 6, 5] ...

Having trouble loading JSON data into HTML using jQuery

I'm struggling to create a basic menu using data from a JSON file. I've been attempting to figure out the correct logic for days now, but I seem to have hit a dead end: Here's the approach I've taken: $.ajax({ url: 'data ...

Tips on displaying a confirmation message upon a user clicking a checkbox

I am encountering an issue with displaying a confirmation message when a checkbox is clicked. The confirmation box pops up but does not carry out any action, like showing the text within the textbox after the user clicks OK. Any guidance on this matter wou ...

Executing Node.js child processes: streaming stdout to console as it happens and running the processes one after the other

Details node: v9.4.0 I am looking for a solution to execute external commands sequentially while monitoring the stdout in real-time. The code snippet below demonstrates my attempt to retrieve all test cases from ./test/ and then running them one after ...

Troubleshooting issue with jQuery datepicker not triggering onselect event

Query Function: $(function() { $("#iDate").datepicker({ dateFormat: 'dd MM yy', beforeShowDay: unavailable onSelect: function (dateText, inst) { $('#frmDate').submit(); } }); }); HTML ...

Encountering the error "Invalid URL. Please provide only absolute URLs" while trying to integrate the Airtable JavaScript library in a Next.js application

I am currently working on a Next JS application that includes a Page called somePage.js. In this page, I am attempting to make an XHR request to the Airtable API from within the getServerSideProps function. The relevant components look like this: pages/so ...