Google Apps script convert .docx file to .pdf - google-apps-script

I am making a Google Apps Script 'Docs' add-in.
I have the following code:
return {
name: DocumentApp.getActiveDocument().getAs('application/pdf').getName(),
base64: Utilities.base64Encode(DocumentApp.getActiveDocument().getAs('application/pdf').getBytes())
}
The getActiveDocument() method is forbidden returning this:
Exception: The document is inaccessible. Please try again later. [line: 130, function: getPDF, file: Code]
This is because google docs works with google doc files and not .docx files.
How could I convert the .docx file I have in Docs to a .pdf file?
Note: copying the file and deleting the previous one is not an option.
Note: I want to do it within the app (in the code). Not manually :)

Open your docx document in MS Word 2013.
2Click on the File tab and select Save as.
3Choose the folder where you want to save the file. In the drop-down menu Save as type, choose PDF. Click the Save button and your docx file will be saved in PDF.

Related

Google Drive stored HTML file as gDoc file, how to get original HTML file back?

I backed up HTML file to Google Drive and when I downloaded it, it's now a gDoc file. I no longer have the original files so now it's wrapped in this gDoc file. I have tried to export using File > Download > Web page (.html, zipped) however, it just downloads an HTML file which is a view of how Google Docs is rendering the document, rather than the original HTML file.
Is there a way to retrieve the original file?
I found that if I open the file in Google Docs and view the Version History, I can find the initial upload: "Imported .html file - View original"
Clicking View original allows me to see that HTML file and download it.

Change the file name for google spreadsheet when downloaded as TSV

I have a published google spreadsheet and would like it to download as a specific file name when I download it as a TSV. The name of the file when downloaded is the name of my sheet + - Template.tsv. I need a way of changing this setting on the google end because this file will be downloaded via an HTML form, so I won't have the option to manually change the file name.

Download a file from a link on another site?

I am attempting to use Google Apps Script to download an excel file that is linked to on a CRM. That is, instead of each day having a person log in to the CRM, download new data as an excel file, and copy and paste the new data to our existing Google Sheet, I would like to have my script do so.
Is it possible to do this? I have been looking at UrlFetchApp, however I'm having trouble because there is no direct link to the file I need (only a link to generate and download the file).

How to copy contents of one script file and replace the contents of another script file?

How can I open a apps script script file, copy its contents and then replace the contents of another apps script script file in the same account with the copied contents?
What I have so far:
// open file to copy from
var file = DriveApp.getFileById(fileIdToCopyFrom);
// copy file contents?
var copiedContents = file.getAs('text/plain'); // generates error: Converting from application/vnd.google-apps.script to application/pdf is not supported.
// replace file contents with copied content
DriveApp.getFileById(fileIdToCopyTo).setContent(copiedContents);
Take a look at Importing and Exporting Projects
You are on the right path using Google Drive, but you need to use the API as described in the documentation.
The key is getting the file data as JSON using the exportLinks this ifo can be pushed into another apps script file.
Also keep in mind that MIME type is application/vnd.google-apps.script

Open download prompt with Google Apps Script

I want to show a download prompt when user hits the Apps Script web app URL.
Here is my code which shows the download prompt. But the downloaded file is an invalid file. The zip file does not open.
I opened the invalid file with text editor and compared the same with original file's content. Although most characters match but some characters were missing.
function doGet(e) {
return ContentService.createTextOutput(DriveApp.getFileById("XXXXXXXXXXXX").getBlob().getDataAsString()).downloadAsFile("Example.zip");
}
If you want to view the file as PDF, you can use this method.
You can also try
return ContentService
.createTextOutput()
.append(getFile(fileName))
.downloadAsFile(fileName);
In getFile() method, you can get the actual file from drive.
Hope that helps!!