This is the code I have:
doc.getBody().editAsText().insertText(0, 'Google').setLinkUrl(0, text.length, 'www.google.com');
This works but inserts the text into the very top of the page. How can I set to where I am in the code?
Found the answer:
var link = body.appendParagraph("Google")
link.setLinkUrl("https://www.google.com");
Related
I have a Google Sheet with form responses. I created some code so that each time a form is completed, the results populate a Google Doc. This works fine.
However, I want to add text to my Google Doc as well, which I accomplish using:
function myFunction(e) {
var doc = DocumentApp.create('File');
var text = 'insert text here';
body.appendParagraph(text);
doc.saveAndClose();
}
This text only is added as plain text, however, and I'd like to format this text. Specifically, I'd like to add the text so that it's bolded, underlined, and center-aligned in the document body.
How do I do this?
After some internet searching and SO searching, I tried adding html (e.g., <b> </b>) and I tried text.setBold(true). These approaches did not work.
I'll admit that I know very little about coding in Google Script editor, so I'm not at all sure how to go about this. I'm lucky enough that I got all my form responses to populate a named Google Doc file!
Here's a fragment of a document that I created recently:
var nameStyle={};
nameStyle[DocumentApp.Attribute.FONT_SIZE]=8;
nameStyle[DocumentApp.Attribute.BOLD]=true;
nameStyle[DocumentApp.Attribute.FOREGROUND_COLOR]='#000000';
var valStyle={};
valStyle[DocumentApp.Attribute.FONT_SIZE]=12;
valStyle[DocumentApp.Attribute.BOLD]=false;
valStyle[DocumentApp.Attribute.FOREGROUND_COLOR]='#cc0000';
body.appendParagraph('Basic Project Data').setAttributes(hdg1Style);
var p1=body.appendParagraph(Utilities.formatString('%s: ','PID')).setAttributes(nameStyle);
var p2=body.appendParagraph(Utilities.formatString('%s',selObj.pid)).setAttributes(valStyle);
p2.merge();
for(var i=0;i<basicDataA.length;i++){
var par1=body.appendParagraph(Utilities.formatString('%s: ',basicDataA[i][0])).setAttributes(nameStyle);
var par2=body.appendParagraph(Utilities.formatString('%s',basicDataA[i][1])).setAttributes(valStyle);
par2.merge();
}
Note, the appendParagraph first and then setAttributes.
The above response got me on the right path (Thanks!). Google hasn't documented this feature very well, and some of the features that should work do not. I had found the info on formatting using offsets but that is incredibly tedious and verbose. Here's the full example method that works to insert pre-formatted text into a Google Doc. Hope it helps someone else. Obviously the 2D array can instead be wired up to be fetched from a form, but this will at least show how the rest of the formatting works.
// Open a document by ID.
var body = DocumentApp.openById('YourDocId').getBody();
var authorAffils = [['Tony Tiger',1],['Micky Mouse',2],['Daffy Duck',3],['Elmo Orange',4]];
var nameStyle={};
nameStyle[DocumentApp.Attribute.FONT_SIZE]=11;
// nameStyle[DocumentApp.TextAlignment.Normal]; // This seems to do nothing
nameStyle[DocumentApp.Attribute.FOREGROUND_COLOR]='#000000';
var affilStyle={};
affilStyle[DocumentApp.Attribute.FONT_SIZE]=11;
// affilStyle[DocumentApp.TextAlignment.SUPERSCRIPT]; // This seems to do nothing
affilStyle[DocumentApp.Attribute.FOREGROUND_COLOR]='#cc0000';
for(var i=0;i<authorAffils.length;i++){
var par1=body.appendParagraph(Utilities.formatString(' %s,',authorAffils[i][0])).setAttributes(nameStyle);
var par2=body.appendParagraph(Utilities.formatString('%s',authorAffils[i][1])).setAttributes(affilStyle);
// I am not entirely clear why alignment only works this way whereas font size and color work the other way.
par1.setTextAlignment(DocumentApp.TextAlignment.NORMAL).merge();
par2.setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT).merge();
}
I´m using run.add_picture() to add a image to my document, but it comes with a 0,30cm spacing around it. I want to remove it so there is no spacing between the text and the image, but I don´t know how to clear this spacing.
Here is the code:
from docx import Document
doc = Document()
doc.add_picture('icon.png')
doc.save('test.docx')
If I understand correctly, you want to insert a floating image instead of an inline one. The code you showed inserts an inline one:
from docx import Document
doc = Document()
doc.add_picture('icon.png')
doc.save('test.docx')
However, if you first create the paragraph you want to insert the picture, you can add it in a specific run:
from docx import Document
doc = Document()
par = doc.add_paragraph('test text 2\n')
r = par.add_run()
r.add_picture(r'icon.png')
r.add_break()
r.add_text('\n continue with text after image.')
doc.save('test.docx')
I have with me the canvas speedometer zip file. My intent is to read, from a text file a single number, and accordingly move the needle on the canvas speedometer as per the number read.
I have tried a few things and all I have managed to do is clunk up things a bit further. Any simple way to get around this, please? Preferably, editing the speedometer.html file such and adding a single button that reads the number from the text file and populates it in the text area provided on the speedometer web page?
Please help.
Edit 1: My bad. I should have included the piece of code I fidgeted around with for a brief while. Here goes the javascript snippet:
function func1()
{
var fileToLoad = document.getElementById("fileToLoad").files[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent)
{
var textFromFileLoaded = fileLoadedEvent.target.result;
document.getElementById("maxvalue").value = textFromFileLoaded;
};
fileReader.readAsText(fileToLoad, "UTF-8");
}
And here's the bit about the 'Extract Value' button:
<input type="file" id="fileToLoad">
<button onclick="func1()">Extract Value</button>
The issue is I am getting sort of a clunky page. One which has a choose file button, and the 'Extract Value' button. And then, there are the begin and start buttons defined in the canvas speedometer API. All I am asking is this: is there a way by which I can have a single button on the page on clicking which the data (a single number) present in the text file gets populated on the ext field in the speedometer web page. This is all I want.
And in case anybody is wondering what the speedometer thingy is, please look here:
{http://www.uibox.in/item/68}
Apologies again for leaving a clearly vague question.
I would like know if and how it is possible to change text attributes such as font, size, etc. in Google Apps script at the cursor's level. The script is bound to a Google doc file. For example, after running the script, the text font will change for anything written after that point while leaving the text written before unchanged. This is to mimic the way built-in styles or font menus behave in Google docs.
Here is what I came up with so far. It seems to change the text font globally in the document instead of applying the changes only to the text written after running the code. Any suggestions?
var cursor = DocumentApp.getActiveDocument().getCursor();
if(cursor){
var element4 = cursor.getElement()
var body = DocumentApp.getActiveDocument().getBody()
if (element4.editAsText) {
body.editAsText().setFontFamily(DocumentApp.FontFamily.CALIBRI);
}
}
The code below changes the FontFamily for the paragraph in which you select a text... it keeps the same style for all what is coming after and preserves everything before.
If you want to go deeper in precision you'll have to play with offsets and work at text level inside the paragraph but I thought this version could be sufficient.
function setStyle() {
var selection = DocumentApp.getActiveDocument().getSelection();
if (!selection) {
DocumentApp.getUi().alert('Cannot find a selection in the document.');
return;
}
var selectedElements = selection.getSelectedElements();
var element = selectedElements[0].getElement().getParent();
element.setFontFamily(DocumentApp.FontFamily.CONSOLAS);
}
The DocumentApp.FontFamily enumeration is now deprecated. You should use string names, like "Consolas" (case sensitive!), instead.
You can visit the setAttributes section of the DocumentApp Reference found here
I have a strange question !
I made a simple create/delete notes form and a script to query them all.But it will not be beautiful if someone inserts a 100KM note and the table shows it all.
I please you to suggest me a script that shows only some content of the row, and the rest when you press something like "view all".
For example :
You need to look into truncating the content string, there's a load of answers available here for both JavaScript and PHP based solutions:
JavaScript: smart way to shorten long strings with javascript
PHP: Shorten string with a "..." body
So you show the truncated string to begin with and then on the full view use the original string.
Depending on how your table in the example is setup you could also try using CSS to truncate and have it add the elipses as well.
CSS: http://css-tricks.com/snippets/css/truncate-string-with-ellipsis/
Well, I think you want something like this:
Live jsfiddle
js
$(function()
{
var maxShow = 50;
$( ".holder" ).each(function()
{
var txt = $(this).text();
txt = txt.substr(0,maxShow)+' ...';
$(this).prev().prev().text(txt);
});
$(".show").click(function()
{
var txt = $(this).next('span').text();
$(this).parent().text(txt);
});
});
html
<div class='txt'><span class='summery'></span>view<span class='holder'> 0 I have a strange question ! I made a simple create/delete notes form and a script to query them all.But it will not be beautiful if someone inserts a 100KM note and the table shows it</span></div>
<div class='txt'><span class='summery'></span>view<span class='holder'> 1 I have a strange question ! I made a simple create/delete notes form and a script to query them all.But it will not be beautiful if someone inserts a 100KM note and the table shows it</span></div>
<div class='txt'><span class='summery'></span>view<span class='holder'> 2 I have a strange question ! I made a simple create/delete notes form and a script to query them all.But it will not be beautiful if someone inserts a 100KM note and the table shows it</span></div>
css
.holder
{
display:none;
}