Google App Script- Docs/images in tables? - google-apps-script

I have searched and searched for the syntax to place an image in a table cell with Google App Script in a Google Doc (not Sheet). This is what I am trying to do:
var resp01 = UrlFetchApp.fetch("http://www.example.com/image01.png");
var resp02 = UrlFetchApp.fetch("http://www.example.com/image02.png");
var cells = [[resp01.getBlob()], [resp02.getBlob()]];
copyBody.appendTable(cells);
This yields simply the word "Blob" in my table.
I can do this all day long with a paragraph:
Body.getChild(x).asParagraph().appendInlineImage(resp01.getBlob());
But for some reason, the "parallel" syntax in a table won't cut it? Does anyone know what I am missing?
Thank You

You need to call TableCell's insertImage() method and provide the child index and the blob source of the image as parameters.
Hope that helps!

Thank You for the input KRR.
I was able to get my project where I wanted it to go, part of which involved inserting images in a table in a Google Doc using Google App Script. After much reading, I settled on the syntax that follows. I hope this helps anyone else trying to learn:
function createDoc() {
var doc = DocumentApp.create('Sample Document');
var body = doc.getBody();
var resp01 = UrlFetchApp.fetch("http://www.cincinnati-oh.gov/cityofcincinnati/assets/Image/Logos/cityofcincinnati.png");
var rowsData = [["City of Cincinnati IMAGE:", ""]];
table = body.insertTable(1, rowsData);
var row = table.getRow(0);
var cell = row.getCell(1);
cell.appendImage(resp01.getBlob());
}

Related

Insert hyperlink with variable into google docs using google scripts

I have a google Form, and the responses populate a response google Sheet, which has a google Script function to generate a google Document using a template.
I am trying to get the address entered in the Form (stored in the response Sheet) to become a hyperlink in the generated Doc.
I have been using the body.replaceText() to replace all the fields I need in the Doc:
body.replaceText("{{Date}}", date);
and its working well, but the address field I would like to become a hyperlink.
I have been trying to do it this way:
body.replaceText("{{Location}}", =HYPERLINK("http://www.google.com/maps/place/'+location+'"));
But that does not become a usable hyperlink, resulting with this in the Doc (please note while it becomes a hyperlink on this page it does not become a hyperlink in Docs):
=HYPERLINK("http://www.google.com/maps/place/myplacenotyours")
I have also tried:
body.replaceText("{{Location}}", location = HYPERLINK("http://www.google.com/maps/place/"+location+));
But this throws up syntax errors.
I have this var:
var location = e.values[2];
So perhaps it better to use that to create another var as a hypertext?
I am now trying:
var loclink = 'Hyperlink("http://www.google.com/maps/place/'+location+'","'+location+'")';
but that doesnt do it either... I'm now starting to think that one can't insert a hyperlink using replace method?
Sorry for the noob question, but I can't figure this out. Can you help me find a solution and put a var into a hypertext link and put that into the Doc as a link?!
Cheers.
Something like this:
function insertLink() {
var pattern = '{{Location}}';
var url = 'https://stackoverflow.com/a/69143679/14265469';
var text = 'how to paste a link';
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var next = body.findText(pattern);
if (!next) return;
var start = next.getStartOffset();
body.replaceText(pattern, text);
body.editAsText().setLinkUrl(start, start+text.length-1, url);
}

getValues/setValues not putting anything in target cell

I am trying to build a data logging workflow using Sheets. I've got a getValue/setValue pair that looks great, but isn't writing to the target cell, and I can't understand why. Here's the code I'm using:
function TESTcopy() {
var srcSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var srcSht = srcSpreadsheet.getSheets()[0];
var tgtSpreadsheet = SpreadsheetApp.openById('1xjjXG-tK3DIkgJTQbkR6XFhOEP5nhaNfqqXiFyMu0AY');
var tgtSht = tgtSpreadsheet.getSheets()[0];
var data = srcSht.getRange(4,2,20,18).getValues();
tgtSht.getRange(345,5,20,18).setValues(data);
}
I've gone over the entire script letter-by-letter, and have Googled and searched on SO for several hours. I know that there's a simple explanation, but I just can't see it.
Does anybody else have any ideas? Thanks in advance!

Is it even possible to copy data from one sheet to another with a script? ImportRange style?

Ive searched for 2 days, can't seem to find the answer or anyone that is even doing something kinda like this. I am trying to bring my employees sheets over to my "Master sheet" with a script. I need to keep the formatting and the notes in the cells.
Best I can find is I can open their spread sheets and copy it to my master, but for 8 employees this will be a pain if I need to do this multiple times a day. I would like to just build a function that I can run my script from the "master sheet" and have it write over the last data in their tab. But I want to be able to do this from within my master sheet. I am the owner of all the sheets but this is becoming a pain.
function copyCell() {
var os = SpreadsheetApp.openById('1n4iFXGuC7yG1XC-UIbuT9VrQ7rJWngPkDCv0vsvDed4');
var sheets = os.getSheets();
var existingNote;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var jobnumber = sheets.getRange("C2:C").getValues();
for (var i = 0; i < jobnumber.length; i++)
if (jobnumber[i][0] == sheets){
existingNote = sheets.getRange("C" + (i+2)).getNote();
sheets.getRange("C" + (i+2)).ss.setNote(existingNote);
}
};
Someone please let me know if it is possible and maybe reference a script I can see to be able to modify to fit my needs. Thanks for any help.
This is a possible solution for copying the notes associated with a range of cells. And pasting them onto a similar-sized range of cells.
To copy the notes you can use getNotes(). Docs here.
To paste the notes you can use setNotes(). Docs here.
Ths sample code in this documentation is pretty good if you need to add it into your script.
If you have a script you'd like to share am happy to help you make it work properly.
Edit
Something like this:
function transferNotes() {
// Get notes (not comments) from the source spreadsheet
var sourceSpreadheet = SpreadsheetApp.openById('ID_of_source_spreadsheet');
var sourceTab = sourceSpreadheet.getSheetByName('Name_source_tab');
var sourceRange = sourceTab.getRange('Source_range_address'); // e.g. "A:D"
var notes = sourceRange.getNotes();
// Post the notes to the target spreadsheet
var targetSpreadheet = SpreadsheetApp.openById('ID_of_target_spreadsheet');
var targetTab = targetSpreadheet.getSheetByName('Name_target_tab');
var targetRange = targetTab.getRange('Target_range_address'); // Should be the same dimensions as sourceRange
targetRange.setNotes(notes);
}

Copy table from one Google Docs file to another

Can anyone provide an example script showing how to copy a table from one Google Doc to another? I have tried multiple variations of "Table.insertTable" and "Table.appendTable", but I cannot get it right.
Solved it! I realized that I had to use the "Copy()" function to get a proper handle to the table. So, the final code looks something like this:
var sourcedoc = DocumentApp.openById(sourceid);
var sourcebody = sourcedoc.getBody();
var tables = sourcebody.getTables();
var table = tables[0].copy();
var destdoc = DocumentApp.openById(destid);
var destbody = destdoc.getBody();
var x = destbody.appendTable(table)

Google Script check a column if contains a certain word

I am new to Google Scripting.
I created a script where Google Spreadsheet should connect to GMail. Everything is working well.
My problem is, I need to check if an email content has the word "ORDER". If yes, it should get the word "ORDER" and the numbers after it.
For example, an email contains "ORDER 90104" in the message body, then the Google script should insert ORDER 90104 to a column in the sheet. An example email could be:
Hi, the customer is requesting for ORDER 90104. Thanks.
Is this even possible? As of now, I used getPlainBody() and I can get all the contents of the email and put it in a column. My plan was to check that column and look for the word ORDER. Though I can't find/end with a solution for it.
Here's my working code getting the email content of the 1st three emails:
function getGMail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var threads = GmailApp.getInboxThreads(0,3);
cell = sheet.getRange("A1");
for (var i = 0; i < threads.length; i++) {
threadMessage = threads[i].getMessages();
var emailContent = threadMessage[0].getPlainBody();
cell.offset(i,0).setValue(emailContent);
}
}
Any advice or comments will be greatly appreciated.
Thanks!
Hello theladydevbugger,
As you didn't gave a lot of code my answer will be quite short: yes you can do that!
How: use a regexp: /ORDER [0-9]+/g.
How to use the regexp: bellow a sample code
var emailBody = yourMail.getPlainBody(); // supposing "yourMail" is the mail you retrieved
var isThereAnyOrder = false;
if(mailBody.search(/ORDER [0-9]+/g)>-1){
isThereAnyOrder=true;
var order=mailBody.match(/ORDER [0-9]+/g)>-1)[0];
Logger.log(order);
// do something with the order
}
else{
// there is no order do nothing
}