I have to convert some HTML code to a Rich Text in order to assign it to a cell of a Google Spreadsheet App, and render it not as pure HTML, but as formatted text.
I am new on these stuffs, so I wish to have some suggestions from you.
Thank you so much!
I believe your goal is as follows.
You want to convert the rich text with HTML to the rich text into a cell of Google Spreadsheet.
You want to achieve this using Google Apps Script.
In this case, how about the following sample script? In this sample script, RichTextApp of a Google Apps Script library is used. I have created this library for managing the rich text between Google Spreadsheet, Google Document, and HTML.
Usage:
1. Install Google Apps Script library.
You can see the detail of this at here.
2. Sample script.
This sample script is from here. This script uses Drive API. So please enable Drive API at Advanced Google services.
function convertHTMLToRichText() {
var html = '###'; // Please set your HTML data. Of course, you can retrieve this from a HTML file on Google Drive.
var sheet = SpreadsheetApp.openById("###").getSheetByName("Sheet1"); // Please set the Spreadsheet ID and sheet name.
// Create Google Document by converting HTML to Google Document as a temporal file.
var blob = Utilities.newBlob(html, MimeType.HTML, "sample.html");
var tempDocId = Drive.Files.insert(
{ title: "temp", mimeType: MimeType.GOOGLE_DOCS },
blob
).id;
// Put the value to a cell as the rich text using the method of "DocumentToSpreadsheet".
var res = RichTextApp.DocumentToSpreadsheet({
range: sheet.getRange("A1"),
document: DocumentApp.openById(tempDocId),
});
console.log(res);
// Remove the temporal file.
DriveApp.getFileById(tempDocId).setTrashed(true);
}
In this sample script, html of HTML data is converted to the rich text and put to a cell ("A1" of "Sheet1") of Google Spreadsheet as the rich text.
Note:
This is a simple sample script. So please modify this for your actual situation.
Reference:
RichTextApp
Related
Just a simple question as titled; I have a pdf file which only contains text then I want to load it and extract text.
I believe your goal is as follows.
You want to retrieve the text data from PDF data including only texts using Google Apps Script.
In this case, how about the following flow?
Convert PDF to Google Document using Drive API as a temporal file.
Export text from the created Google Document.
When this is reflected in a script, it becomes as follows.
Sample script:
In this sample, Drive API is used. So, before you test this script, please enable Drive API at Advanced Google services.
function myFunction() {
const fileId = "###"; // Please set the file ID of PDF file on Google Drive.
// Convert PDF to Google Document.
const docId = Drive.Files.copy({title: "temp", mimeType: MimeType.GOOGLE_DOCS}, fileId).id;
// Retrieve text from Google Document.
const text = DocumentApp.openById(docId).getBody().getText();
// If you want to remove the template Google Document, please run this script.
// Drive.Files.remove(docId);
console.log(text); // You can see the retrieved text in the log.
// DriveApp.createFile("sample.txt", text); // If you want to save the text as a file, please use this line.
}
References:
Files: copy
getText()
Note: consider checking the edits first if you have a similar problem
I have Link, Lable, Text and Formula as input
and the formula uses Link, Lable, Text as an input, like this
Make a copy of my example sheet.
=Function(HYPERLINK(A3,B3)," ",C3)
I want to create a custom Function to get the result like in E3
Hyperlink & Text, Google Text, after reading suggested answers i concluded there is no way to achive this result by creating a custom formula that can format the output.
the next best thing is to have a script that extract the formula parameters A3, B3 and the tailing text and use it to output the result in the next cell either automatic onedit or with menu botton.
I tested this script but the problem is the formula is replaced by Plain text only, see the github project google-apps-script-projects. or Make a copy of my example sheet the script is included.
Building to what #Tanaike answer, storing the parameters of the formatting in the Custom formula like this and feed it to the script to output the result in the next cell.
=CustomFunction([A3,B3],C3...)
Explanation
=CustomFunction([Hyperlink,Lable],text...)
I believe your goal is as follows.
Put a text to a cell. In this case, use a hyperlink in a part of the text.
You are required to achieve this using a custom function like =CustomConcatenationFunction(Hyperlink(Link,Lable),"Text1","Text2"...).
In the current stage, in order to reflect the hyperlink in a part of the text, it is required to use setRichTextValue of Google Apps Script. In this case, this method cannot be used with the custom function. This is the current specification.
And, in the case of a custom function like =CustomConcatenationFunction(Hyperlink(Link,Lable),"Text1","Text2"...), the arguments at the custom function side are label, "Text1" and "Text2". I think that in this case, the URL cannot be retrieved at the custom function. I think that this is also a modification point.
So, in order to achieve your goal, it is required to use a workaround. In this post, I would like to introduce the workaround. This workaround uses Web Apps. When Web Apps is used, the methods which cannot be used with a custom function can be used with a custom function. This can be seen at this report and Error when running Youtube Data Service in App Scripts (js) – Daily Limit for Unauthenticated Use Exceeded.
When Web Apps is used for achieving your goal, it becomes as follows.
Usage:
1. Prepare Google Spreadsheet.
Please create a Google Spreadsheet.
2. Prepare sample script.
Please open the script editor of Spreadsheet and copy and paste the following sample script.
function doGet(e) {
const { range, sheetName, link, text, allText } = e.parameter;
const idx = allText.indexOf(text);
const r = SpreadsheetApp.newRichTextValue()
.setText(allText)
.setLinkUrl(idx, idx + text.length, link)
.build();
SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName(sheetName)
.getRange(range)
.setRichTextValue(r);
return ContentService.createTextOutput();
}
// This is used as the custom function.
function SAMPLE(link, text, allText) {
const webAppsUrl = "https://script.google.com/macros/s/###/exec"; // Please set the URL of Web Apps after you set the Web Apps.
const range = SpreadsheetApp.getActiveRange();
UrlFetchApp.fetch(
`${webAppsUrl}?range=${range.getA1Notation()}&sheetName=${range
.getSheet()
.getSheetName()}&link=${link}&text=${text}&allText=${allText}`
);
}
Here, webAppsUrl is required to be replaced with your Web Apps URL. Web Apps is deployed in the following flow.
3. Deploy Web Apps.
The detailed information can be seen at the official document.
Please set this using the new IDE of the script editor.
On the script editor, at the top right of the script editor, please click "click Deploy" -> "New deployment".
Please click "Select type" -> "Web App".
Please input the information about the Web App in the fields under "Deployment configuration".
Please select "Me" for "Execute as".
Please select "Anyone" for "Who has access".
Please click "Deploy" button.
Copy the URL of the Web App. It's like https://script.google.com/macros/s/###/exec, and replace webAppsUrl in the above sample script.
Reflect the latest script to the Web Appps. Because the script of Web Apps is changed. This is an important point.
When you modified the Google Apps Script, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful about this.
You can see the detail of this in the report "Redeploying Web Apps without Changing URL of Web Apps for new IDE".
4. Testing.
In order to test the above sample, please put a custom function like =SAMPLE("###URL###","sampleLink","sampleText sampleLink sampleText"). By this, sampleLink of sampleText sampleLink sampleText has the hyperlink as follows.
Note:
In this case, the inputted custom function is overwritten by the RichTextValue. Because in the current stage, the RichTextValue cannot be used in a custom function.
This is a simple sample script. So, please modify this for your actual situation.
References:
Enhanced Custom Function for Google Spreadsheet using Web Apps as Wrapper.
This sample is for this thread in Stackoverflow
Added:
From we need a workaround to keep the formula in place eiather in a seprate cell or in the formatted cell, I understood you perfectly. we have half of the question answered the last bit is to keep the formula extract the url, lable plain text from it and output the formatted result to a cell on the right as a workaround., how about the following sample script?
In this sample script, the simple trigger of OnEdit is used.
Sample script:
const SAMPLE = _ => "Done";
function onEdit(e) {
const customFunction = "=SAMPLE";
const { range } = e;
const formula = range.getFormula();
if (!formula.includes(customFunction)) return;
const arguments = formula.match(/\((.+)\)/);
if (!arguments) return;
const [link, text, allText] = arguments[1].replace(/"/g, "").split(",");
const idx = allText.indexOf(text);
const r = SpreadsheetApp.newRichTextValue().setText(allText).setLinkUrl(idx, idx + text.length, link).build();
range.offset(0, 1).setRichTextValue(r);
}
When you use this script, please put a custom function of =SAMPLE("###URL###","sampleLink","sampleText sampleLink sampleText") to a cell. By this, the script of onEdit is automatically run by the trigger.
Testing:
When this script is used, the following result is obtained.
I need using the link to download the Google Sheet to .xlxs format.
This is works for me. but it is for single tab only, the thing is I have to download three or more tabs. I believe the format of "gid" would be different.
https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=1eVcHMWyH1YIDN_i0iMcv468c4_jnPk9Tw5gea-2FCyk&gid=626501804&exportFormat=xlsx
I believe your goal is as follows.
You want to export a Google Spreadsheet in XLSX format.
You want to include several specific Sheets in the Google Spreadsheet.
In this case, how about the following workaround? In this workaround, a Google Spreadsheet including the several sheets you want to include is created as a temporal Spreadsheet. In this case, as a simple method, Google Apps Script is used for retrieving the URL.
Sample script:
function sample() {
const exportSheetNames = ["Sheet1", "Sheet2", "Sheet3"]; // Please set the sheet names you want to export.
const spreadsheetId = "###"; // Please set your Spreadsheet ID.
const source = SpreadsheetApp.openById(spreadsheetId);
const temp = source.copy("temp_" + source.getName());
temp.getSheets().forEach(s => {
if (!exportSheetNames.includes(s.getSheetName())) temp.deleteSheet(s);
});
const url = `https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=${temp.getId()}&exportFormat=xlsx`;
console.log(url);
}
When this script is run, you can see the URL for exporting the Spreadsheet in XLSX format including the specific sheets you want at the log. From your question, I thought that you might want the URL for exporting.
This is a simple sample script for achieving your goal. For example, if you want to automatically export the XLSX file using a script, you can see the sample script at this thread.
I have a bunch of HTML files in the google drive, but I need to extract tables from them and put into Gsheets.
So far I saw ImportHTML function but it does not work with the drive link.
How can I import and parse HTML files from my Drive? Thank you
You want to put the values of the table from HTML data using Google Apps Script and/or the built-in functions of Spreadsheet.
The HTML files are put in your Google Drive.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Pattern 1:
In this pattern, IMPORTXML is used for the tables deployed with Web Apps.
Usage:
1. copy and paste the following script to the script editor.
function doGet(e) {
var fileId = e.parameter.id;
var html = DriveApp.getFileById(fileId).getBlob().getDataAsString();
var html = "<sample>" + html.match(/<table[\w\s\S]+?<\/table>/gi).join("") + "</sample>";
return ContentService.createTextOutput(html).setMimeType(ContentService.MimeType.XML);
}
2. Deploy Web Apps.
On the script editor, Open a dialog box by "Publish" -> "Deploy as web app".
Select "Me" for "Execute the app as:".
Select "Anyone, even anonymous" for "Who has access to the app:".
Click "Deploy" button as new "Project version".
Automatically open a dialog box of "Authorization required".
Click "Review Permissions".
Select own account.
Click "Advanced" at "This app isn't verified".
Click "Go to ### project name ###(unsafe)"
Click "Allow" button.
Click "OK".
Copy the URL of Web Apps. It's like https://script.google.com/macros/s/###/exec.
When you modified the Google Apps Script, please redeploy as new version. By this, the modified script is reflected to Web Apps. Please be careful this.
3. Put the formula.
Please put the following formula to a cell.
=IMPORTXML("https://script.google.com/macros/s/###/exec?id=###fileId###","//tr")
###fileId### is the file ID of HTML file on Google Drive.
Pattern 2:
In this pattern, the HTML tables are retrieved from the HTML data, and the tables are put to the Spreadsheet using Sheets API.
Usage:
1. copy and paste the following script to the script editor.
Please set the variables of fileId, spreadsheetId and sheetName.
function myFunction() {
var fileId = "###"; // Please set the file ID of HTML file.
var spreadsheetId = "###"; // Please set the Spreadsheet ID for putting the values.
var sheetName = "Sheet1"; // Please set the sheet name for putting the values.
// Retrieve tables from HTML data.
var html = DriveApp.getFileById(fileId).getBlob().getDataAsString();
var values = html.match(/<table[\w\s\S]+?<\/table>/gi);
// Put the HTML tables to the Spreadsheet.
var ss = SpreadsheetApp.openById(spreadsheetId);
var sheet = ss.getSheetByName(sheetName);
var sheetId = sheet.getSheetId();
var rowIndex = 0;
values.forEach(function(e) {
var resource = {requests: [{pasteData: {html: true, data: e, coordinate: {sheetId: sheetId, rowIndex: rowIndex}}}]};
Sheets.Spreadsheets.batchUpdate(resource, spreadsheetId);
rowIndex = sheet.getLastRow();
})
}
2. Enable Sheets API.
Please enable Sheets API at Advanced Google services.
3. Run the script.
When you run the function myFunction, the values are retrieved from HTML data and they are put to the Spreadsheet.
Note:
These are the simple sample scripts. So please modify them for your actual situation.
References:
Web Apps
Taking advantage of Web Apps with Google Apps Script
Advanced Google services
spreadsheets.batchUpdate
Unfortunately, from your question, I cannot understand about your actual HTML data. So if an error occurs and this was not the direction you want, I apologize.
Data Source
https://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yieldAll
I am trying to get the following data onto a Google Sheet, but it is looking to be tricky to do so using IMPORTXML. Any idea how to do it?
You want to retrieve a table from the HTML data of the URL.
From I am trying to get the following data onto a Google Sheet, I thought like this.
If my understanding is correct, how about this answer?
Issue and workaround:
Unfortunately, it seems that the file size of HTML is large. So when =IMPORTXML("https://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yieldAll","//title") is used, an error of Resource at url contents exceeded maximum size. occurs. When I retrieve HTML data from the URL, the size of HTML data was about 9 MB. It is considered that the reason of error is due to this. So as one of workaround, how about using Google Apps Script? In this workaround, the following flow is used.
Retrieve HTML data using UrlFetchApp
Parse the HTML data using Parser which is a GAS library.
Put the parsed data to the active sheet on the Spreadsheet using PasteDataRequest of Sheets API.
Usage:
Preparation:
Please install Parser. About the install of library, you can see it at here.
The project key of the library is M1lugvAXKKtUxn_vdAG9JZleS6DrsjUUV.
Please enable Sheets API at Advanced Google services.
Sample script:
Please copy and paste the following script to the script editor of the container-bound script of the Spreadsheet. After above settings were done, please run the function of myFunction(). When the script is run, the table of HTML is put to the active sheet on the Spreadsheet.
function myFunction() {
// Retrieve HTML data from URL.
var url = "https://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yieldAll";
var html = UrlFetchApp.fetch(url).getContentText();
// Parse HTML data.
var table = "<table" + Parser.data(html).from("<table class=\"t-chart\"").to("</table>").build() + "</table>";
// Put the values to the Spreadsheet.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var resource = {requests: [{pasteData: {html: true, data: table, coordinate: {sheetId: sheet.getSheetId()}}}]};
Sheets.Spreadsheets.batchUpdate(resource, ss.getId());
}
References:
Parser
PasteDataRequest
Advanced Google services
If I misunderstood your question and this was not the direction you want, I apologize.
Updated at April, 23, 2021:
New IDE for Google Apps Script has finally been released at December 7, 2020. Ref By this, in the current stage, in order to install Google Apps Script library, it is required to use the script ID of Google Apps Script project.
In this case, when the Google Apps Script library of Parser is installed, unfortunately, this ID M1lugvAXKKtUxn_vdAG9JZleS6DrsjUUV cannot be used.
So when you use new IDE, please use the following script ID.
1Mc8BthYthXx6CoIz90-JiSzSafVnT6U3t0z_W3hLTAX5ek4w0G_EIrNw
This script ID is the ID of Google Apps Script project of M1lugvAXKKtUxn_vdAG9JZleS6DrsjUUV. By this, the library of Parser can be installed to the new IDE.
About the method for installing the library, you can see the official document.
Reference:
Libraries