Get the hyperlink and its text value using google docs - google-apps-script

I hope everyone is in good health.
My main goal
So main goal was to get the hyperlink and the text linked with it. I initially used code as below :
var ekArr = [];
function myFunction() {
var doc= DocumentApp.getActiveDocument();
var isCell4Blank1 = doc.getBlob().getDataAsString();
var data = Utilities.parseCsv(isCell4Blank1);
var linku = doc.getBody().getTables()[0].getCell(2,1);
var t = doc.getBody().getTables()[0].getCell(2,1).getText();
Logger.log("link is. " + linku);
// Logger.log(doc.getBody().getText());
for(var find in data){
var fi = data[find].toString().includes("http");
if(fi===true){
var newArr = data[find].toString().split(" ");
var output = newArr[1].replace("(","").replace(")","").replace(">>>>","")
ekArr.push(output);
}
}
var tex = doc.getBody().getText();
var fo = tex.split(ekArr[0]);
Logger.log(tex);
doc.getBody().getTables()[0].getCell(2,2).setText(ekArr);
doc.getBody().getTables()[0].getCell(2,3).setText(t);
Logger.log(ekArr);
return ekArr;
}
I need to extract the links and content from the cells of a table.
From above code I am able to extract the links however I am not able to extract the text linked with it.
Also I have another code which helped me to extract the links and text but from googlesheets. As I am new to google docs I want to modify the below code according to google docs.
function sheetFunction() {
var sheet= SpreadsheetApp.getActiveSheet();
var isCell4Blank1 = sheet.getRange("A1").isBlank();
if (!isCell4Blank1) {
var linkData =
sheet.getRange("A1").getRichTextValue().getRuns().reduce((ar, e) => {
var url = e.getLinkUrl();
Logger.log(url);
if (url) {
var color = e.getTextStyle().getForegroundColor();
var startIndex = e.getStartIndex();
var endIndex = e.getEndIndex();
var text = e.getText()
ar.push(color);
ar.push(text);
ar.push(startIndex);
ar.push(endIndex);
ar.push(url);
}
return ar;
}, [])
}

Use the methods getText() and getLinkUrl()
Sample based on your first code snippet:
function myFunction() {
var doc = DocumentApp.getActiveDocument();
var linku = doc.getBody().getTables()[0].getCell(2,1);
Logger.log("link text is: " + linku.getText());
Logger.log("link is: " + linku.getLinkUrl());
}

Related

How to add hyperlink in table (or array) in a Google Doc using Google DOC API Script?

I am having the following code for appending table in Google Docs.
var sss = SpreadsheetApp.openById('id of spreadsheet');
var rawData = sss.getDataRange().getValues()
var data = []
for (var i = 0; i< rawData.length; i++){
tempData=[]
tempData=[rawData[i][1],rawData[i][2],rawData[i][3]]
data.push(tempData)
}
var someDoc = DocumentApp.openById(someId);
var body = someDoc.getBody();
body.appendTable(data).editAsText().setBold(false);
This code works fine. The problem is that there is url in rawdata[i][3]. It gets displayed in Google doc as plain text. How can I convert it into hyperlink? It would be even better if it is possible to write it as body.appendParagraph("my link").setLinkUrl("http://www.google.com"). The problem is that it is in an array, not in paragraph.
I believe your goal is as follows.
You want to put a table to Google Document by retrieving the values from Google Spreadsheet.
In your Spreadsheet, the column "D" has the hyperlinks. And, you want to set the value as the hyperlink.
In this case, how about the following modification?
Modified script:
var sss = SpreadsheetApp.openById('id of spreadsheet');
var rawData = sss.getDataRange().getValues();
var data = []
for (var i = 0; i < rawData.length; i++) {
tempData = []
tempData = [rawData[i][1], rawData[i][2], rawData[i][3]]
data.push(tempData)
}
var someDoc = DocumentApp.openById(someId);
var body = someDoc.getBody();
// I modified below script.
var table = body.appendTable(data);
for (var r = 0; r < table.getNumRows(); r++) {
var obj = table.getCell(r, 2).editAsText();
var text = obj.getText();
if (/^https?:\/\//.test(text)) obj.setLinkUrl(text);
}
table.editAsText().setBold(false);
When this script is run, a table is put using the values retrieved from Spreadsheet. And, about the column "C" of the table, the text is changed to the hyperlink.
Note:
This modified script supposes that your values of column "D" are like https://### and http://###. Please be careful about this.
If you want to give the specific text (for example, click here) with the hyperlink, please modify as follows.
From
if (/^https?:\/\//.test(text)) obj.setLinkUrl(text);
To
if (/^https?:\/\//.test(text)) obj.setText("click here").setLinkUrl(text);
References:
getCell(rowIndex, cellIndex) of Class Table
setLinkUrl(url)
You can try the following to make all URLs in a Google Doc clickable:
function urlClickable() {
var urlRegex = 'http[s]?:\/\/[^ ]+';
var doc = DocumentApp.openById('yourDocId');
var body = doc.getBody();
var urlElement = body.findText(urlRegex);
while (urlElement != null) {
var text = urlElement.getElement().asText();
var startOffset = urlElement.getStartOffset();
var endOffset = urlElement.getEndOffsetInclusive();
text.setLinkUrl(startOffset, endOffset, getUrl(text.getText()));
urlElement = body.findText(urlRegex, urlElement);
}
}
function getUrl(text) {
var startOffset = text.indexOf('http');
var endOffset = text.indexOf(' ', startOffset);
if (endOffset === -1) {
endOffset = text.length;
}
return text.substring(startOffset, endOffset);
}

Google sheets OCR image at URL in Col1, output text in Col2?

in a Google Sheet, I'm trying to use a function to OCR an image whose URL is in column A, and output the OCR text in column B. The code I'm attempting to use is from this thread:
ocr images from list of urls and store the results in spreadsheet
function onOpen() {
var ss = SpreadsheetApp.getActive();
var menuItems = [
{name: 'RUN', functionName: 'doGet2'}
];
ss.addMenu('OCR', menuItems);
}
function doGet2() {
var ROW_START = 3;
var URL_COL = 1;
var TEXT_COL = 2;
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var urls = sheet.getRange(ROW_START,URL_COL, sheet.getLastRow()-ROW_START+1,1).getValues();
var texts = [];
for(var i=0; i<urls.length; i++) {
var url = urls[i];
if(url != undefined && url != "") {
var imageBlob = UrlFetchApp.fetch(url).getBlob();
var resource = {
title: imageBlob.getName(),
mimeType: imageBlob.getContentType()
};
var options = {
ocr: true
};
var docFile = Drive.Files.insert(resource, imageBlob, options);
var doc = DocumentApp.openById(docFile.id);
var text = doc.getBody().getText().replace("\n", "");
texts.push([text]);
Drive.Files.remove(docFile.id);
}
else {
texts.push("request error");
}
}
sheet.getRange(ROW_START,TEXT_COL, urls.length,1).setValues(texts);
}
Here is the Google Sheet I'm testing with. Anyone can edit:
https://docs.google.com/spreadsheets/d/1jhSmE295bTOzjoXmgcyymNi-6ylKg5PLEKktPZJTD1c/edit?usp=sharing
I think the original code is meant to pull from Google Drive. I don't need that, I just want to pull from a URL. Being able to use the function inside an arrayformula would be a plus.
Thanks!

Paste Google Docs URLs into Google Sheets and automatically get title and word count

Probably a long shot, but does anyone know of any formulae or scripts that could be used with Google Sheets so that when you paste in a selection of URLs to Google sheets, it would automatically get the title and word count for each URL?
What you describe could, a priori, be implemented using custom functions. It would query the Docs API for the desired metadata and return it into the cell.
This, however, is not possible since access to apis such as DocumentApp is not allowed from custom functions (1). Upon attempting to do so, they will return an error such as the following:
You do not have permission to call DocumentApp.openByUrl.
Required permissions: https://www.googleapis.com/auth/documents (line 88).
Alternative
Insert the =GETDOCWORDCOUNT() and =GETDOCTITLE() functions as you please throughout your Sheets document. At first, they will show #ERROR! as result of the function, for the reason explained above:
The code, when opening the Sheets document, creates a "Custom scripts" menu. After having placed your formulas in the document, click on the menu and select the Run GETDOCWORDCOUNT or Run GETDOCTITLE as you wish. The formulas in the Sheet that were previously shown as #ERROR! will be replaced by the result of running the functions. At this step, any error found whilst executing the GETDOCWORDCOUNT, GETDOCTITLE functions will also be shown to the user:
Code
var GETDOCWORDCOUNT_FUNCTION_REGEX = /=GETDOCWORDCOUNT\((.+)\)/;
var GETDOCTITLE_FUNCTION_REGEX = /=GETDOCTITLE\((.+)\)/;
var A1_CELL_REGEX = /^[A-Z]+[1-9][0-9]*$/;
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu("Custom scripts")
.addItem("Run GETDOCWORDCOUNT", "runGetDocWordCount")
.addItem("Run GETDOCTITLE", "runGetDocTitle")
.addToUi();
}
function runGetDocWordCount() {
var sh = SpreadsheetApp.getActiveSheet();
var finder = sh.createTextFinder("\=GETDOCWORDCOUNT\(.+)")
.matchCase(true)
.matchEntireCell(true)
.useRegularExpression(true)
.matchFormulaText(true);
var results = finder.findAll();
var errors = [];
for (var i=0; i<results.length; i++) {
var range = results[i];
var formula = range.getFormula();
var cell = formula.match(GETDOCWORDCOUNT_FUNCTION_REGEX)[1];
var url = sh.getRange(cell).getValue();
try {
range.setValue(GETDOCWORDCOUNT(url));
} catch(e) {
errors.push(range.getA1Notation() + ': ' + e.toString());
}
}
if (errors.length > 0) {
var ui = SpreadsheetApp.getUi();
var title = errors.length.toString() + ' errors found';
var prompt = errors.join('\n');
ui.alert(errors.length + ' errors found', prompt, ui.ButtonSet.OK);
}
}
function runGetDocTitle() {
var sh = SpreadsheetApp.getActiveSheet();
var finder = sh.createTextFinder("\=GETDOCTITLE\(.+)")
.matchCase(true)
.matchEntireCell(true)
.useRegularExpression(true)
.matchFormulaText(true);
var results = finder.findAll();
var errors = [];
for (var i=0; i<results.length; i++) {
var range = results[i];
var formula = range.getFormula();
var cell = formula.match(GETDOCTITLE_FUNCTION_REGEX)[1];
var url = sh.getRange(cell).getValue();
try {
range.setValue(GETDOCTITLE(url));
} catch(e) {
errors.push(range.getA1Notation() + ': ' + e.toString());
}
}
if (errors.length > 0) {
var ui = SpreadsheetApp.getUi();
var title = errors.length.toString() + ' errors found';
var prompt = errors.join('\n');
ui.alert(errors.length + ' errors found', prompt, ui.ButtonSet.OK);
}
}
function GETDOCWORDCOUNT(url) {
var doc = DocumentApp.openByUrl(url);
var text = doc.getBody().getText();
var words = text.split(/\S+/).length;
return words;
}
function GETDOCTITLE(url) {
var doc = DocumentApp.openByUrl(url);
return doc.getName();
}
function isA1Cell(val) {
if (typeof val != "string") return false;
return A1_CELL_REGEX.test(val);
}
Demo
See a short video demo of the script here
Try this:
function getIdTitleCount(url) {
var id=url.slice(url.indexOf('/d/')+3,url.indexOf('/edit'));
var title=DriveApp.getFileById(id).getName();
var count=DocumentApp.openById(id).getBody().getText().split(/\s/).length;//This probably could be a better regex but by my count it works pretty well for simple text.
Logger.log(id);
Logger.log(title);
Logger.log(count)
}

Get range of google spreadsheet into an image using google script

I'm building a search tool by which one can convert a google range into an image using google script. I tried to paste that data range to google slides and from there I get thumbnails and their url. I'm searching any other tools that directly give me the url of image of the selected range of a google sheet.
Thanks
This is a very interesting question.
I am unsure of the reasoning behind doing this, but nonetheless, here is an answer:
This creates a custom menu on the top of your sheet that says:
Custom Functions => Export Range to Image Files.
When you click that it:
Turns whatever you have selected into a table in sheets
Saves it
Generates an image from that
Saves image to drive
Show a pop-up with the links of the saved images.
The code is ready to handle multiple ranges being exported, but right now it exports only the selected range.
function onOpen(e) {
//Create custom menu to export range to Slides.
SpreadsheetApp.getUi()
.createMenu('Custom Functions')
.addItem('Export Range to Image Files', 'SelectedRangeToImage')
.addToUi();
}
function SelectedRangeToImage() {
var slide = RangeToSlides();
var slideId = slide.getId();
var images = [];
for (var x=0; x<slide.getSlides().length;x++) {
var image = SlidesToImage(slide.getName()+x, slideId, slide.getSlides()[x].getObjectId());
images.push(image);
}
//Show interface with links to all images
var ui = SpreadsheetApp.getUi();
var html = HtmlService.createHtmlOutput();
html.append("<p>Your images:</p>");
html.append("<ul>");
for (var i=0; i<images.length; i++) {
html.append("<li><a href='"+images[i].getUrl()+"'>"+images[i].getName()+"</a></li>");
}
html.append("</ul>");
html.append("<input type='button' value='Close' onclick='google.script.host.close()' />");
ui.showModalDialog(html, "Exporting results:");
}
function RangeToSlides() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getActiveRange();
var rangeValues = range.getDisplayValues();
var rangeHorizontalAlignments = range.getHorizontalAlignments()
var rangeBackgrounds = range.getBackgrounds();
var rangeFontWeights = range.getFontWeights();
var sl = SlidesApp.create("ExportToImage"+new Date());
var slide = sl.getSlides()[0];
//Create table with size of the range
var table = slide.insertTable(rangeValues.length, rangeValues[0].length);
for (var x=0; x<rangeValues.length; x++) {
for (var y=0; y<rangeValues[x].length; y++) {
var cell = table.getCell(x,y);
cell.getText().setText(rangeValues[x][y]); //Set text
cell.getFill().setSolidFill(rangeBackgrounds[x][y]); //Set background
cell.getText().getTextStyle().setBold(rangeFontWeights[x][y]=="bold"?true:false); //Set text formatting
var alignment;
switch(rangeHorizontalAlignments[x][y]) {
case "general-left":
alignment = SlidesApp.ParagraphAlignment.START;
break;
case "general-right":
alignment = SlidesApp.ParagraphAlignment.END;
break;
case "center":
alignment = SlidesApp.ParagraphAlignment.CENTER;
break;
}
cell.getText().getParagraphStyle().setParagraphAlignment(alignment); //Set text alignment
}
}
sl.saveAndClose();
return sl;
}
function SlidesToImage(name, presentationId, slideId) {
var url = "https://slides.googleapis.com/v1/presentations/"+presentationId+"/pages/"+slideId+"/thumbnail";
var options = {
headers: {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
}
};
var response = UrlFetchApp.fetch(url, options);
var responseJson = JSON.parse(response.getContentText());
var imageurl = responseJson.contentUrl;
var imageResponse = UrlFetchApp.fetch(imageurl, options);
var blob = imageResponse.getBlob();
blob.setName(name);
var resultingFile = DriveApp.createFile(blob);
return resultingFile;
}
Hope this helps!
References:
https://stackoverflow.com/a/51391196/11869748
How to download Google Slides as images?
https://developers.google.com/slides/reference/rest/v1/presentations.pages/getThumbnail

Translate Google Doc from List - Problem: does not fully run on some docs

I created some simple code that pulls a Google Doc URL from a spreadsheet in cell B2. The code works for some documents, but not others.
The program correctly creates a duplicate of the doc, which preserves all formatting. From there, I aim to replace the English text with Spanish.
I run into errors with Http:// links and some documents fully reformat, others do not.
Suggestions?
//This function works to create a nearly perfect copy of the document from English to Spanish. Problems with URLs and some long docs.
function spanishConverter() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); //bound to Google Sheet
var listDoc = ss.getSheetByName('Doc List'); //SpreadSheet Page
var docEng = listDoc.getRange(2,2).getValue(); //Cell pulling URL from
var currentDoc = DocumentApp.openByUrl(docEng); //Opens the English version from URL on SpreadSheet
var docName = currentDoc.getName();
var docId = currentDoc.getId();
var newDoc = DriveApp.getFileById(docId).makeCopy('Spanish ' + docName).getUrl(); //Opens and makes a copy and captures URL
var newSp = DocumentApp.openByUrl(newDoc);
var body = newSp.getBody();
var paragraphs = body.getParagraphs();
//This script loops throught the paragraphs
for(var i=0; i<paragraphs.length; i++)
{
var text = paragraphs[i].getText(); //pulls text per paragraph
var transText = LanguageApp.translate(text, 'en', 'es');
Logger.log([text,transText]);
if (text !== "") //skips blanks
{
body.replaceText(text, transText);
}
}
}
This is the full solution. HTML links are skipped unless they are interpreted as a text element.
// This function works to create a nearly perfect copy of the document
// from English to Spanish.
function spanishConverter() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); //bound to Google Sheet
var listDoc = ss.getSheetByName('Doc List'); //SpreadSheet Page
var docEng = listDoc.getRange(2,2).getValue(); //Cell pulling URL from
var currentDoc = DocumentApp.openByUrl(docEng); //Opens the English version from URL on SpreadSheet
var docName = currentDoc.getName();
var docId = currentDoc.getId();
var newDoc = DriveApp.getFileById(docId).makeCopy('Spanish - ' + docName).getUrl(); //Opens and makes a copy and captures URL
var newSp = DocumentApp.openByUrl(newDoc);
var body = newSp.getBody();
var paragraphs = body.getParagraphs();
//This script loops throught the paragraphs
for(var i=0; i<paragraphs.length; i++)
{
var para = paragraphs[i];
var text = para.getText(); //pulls text per paragraph
var atts = para.getAttributes(); //pulls formatting attributes to set when appending text
var transText = LanguageApp.translate(text, 'en', 'es');
Logger.log([atts]);
if (text !== "") //skips blanks
{
para.clear();
para.appendText('{sp}').setAttributes(atts);
para.replaceText('{sp}', transText);
}
}
}