Making links from script in google spreadsheet - google-apps-script

I want to make some links in my spreadsheet. I can make a string that looks like this. I looked around, though I did only find methods to link links that are already in the spreadsheet.
var linkBattlenet = "http://eu.battle.net/wow/en/character/"+toonrealm+"/"+toon.name+"/advanced"
var showLink = "battle.net link"+toon.name //this is what shall be seen in the spreadsheet.
I tried
var showLink = "battle.net link"+toon.name
var linkBattlenet = showLink.link("http://eu.battle.net/wow/en/character/"+toonrealm+"/"+toon.name+"/advanced")
Here i got the problem that it printed a string into the spreadsheet cell, that looks like this:
"%Character name%" .
(I am returnig the value in an array)

Instead of using showLink.link(), you can directly assign the string value to the cell.
Tried this code below:
function makeLink(){
var name = "john";
var showLink = ""http://eu.battle.net/wow/en/character/+name+"/advanced"";
var mainsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Main');
mainsheet.getActiveCell().setValue(showLink);
}
If you don't want to see the complete url in the cell, you can also try this line instead:
var showLink = '=HYPERLINK("http://eu.battle.net/wow/en/character/'+name+'/advanced","clickhere")';
Hope that helps!

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);
}

Get URL of a file in Google Drive thanks to its name

First of all, sorry for my English (I'm learning ^^).
I want to have a script to find a file's url thanks to the name of the file, and then put the URL in my sheet called 'Suivi-des-devis!' in the range 'O2'
The files are always called "FAC_quote number", and the quote number is in sheet 'DEVIS!' in 'E6'.
I really tried to create the script alone, but it doesn't work. Can someone help me to correct the script please ?
function testenregistrement() {
const doc = SpreadsheetApp.getActive();
var f2 = doc.getSheetByName('Suivi-des-devis');
var dossier2 = DriveApp.getFolderById("1WNyBdexfylgflnDeKtMcdIN-eS578dlH")
var nom = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('DEVIS').getRange('E6').getValue();
var files = dossier2.getFilesByName("FAC_" + nom).getUrl(); f2.getRange('O2').setValue(files);
}
The Folder.getFilesByName() method gets a FileIterator object. Use the FileIterator.next() method to get a file, like this:
var files = dossier2.getFilesByName("FAC_" + nom).next().getUrl();

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)

How to get a list of files matching a cell value in GAS?

I'm hoping to make (what I hoped was) a very basic script, where you can type part of a document name into a Google Spreadsheet, and underneath will appear the files in your Drive that have that word in their title.
As an example, I have two files in my Drive called "Rome Adventure" and "London Adventure", and the idea would be that if you typed "Rome", "London", or "Adventure" into a cell, the file titles would appear below.
So far I've got this:
function onEdit(e){
// Gets the edited cell, turns it into string
var activeSheet = e.source.getActiveSheet();
var range = e.range;
var input = range.getCell(1,1).getValue();
var SearchString = 'title contains "' + input + '"';
// Searches Drive for files with titles containing whatever you typed
// and appends titles to the spreadsheet
var result = DriveApp.searchFiles(SearchString);
while (result.hasNext()) {
var file = result.next();
activeSheet.appendRow([file.getName()]);
}
}
But unfortunately nothing is appended, regardless of what I enter. I've tried "Rome", "London", and just about everything else I can think of. Thinking I might have stuffed up something in the first section, I added
range.setNote('Last modified: ' + new Date());
in between the two sections, and that worked. So it's definitely just the DriveApp.searchFiles that I've stuffed up. I thought I might have stuffed up the StringSearch bit, but changing the line to
var result = DriveApp.searchFiles('title contains "Rome"');
still doesn't return anything.
I'm only an enthusiast-level programmer, and this is the first time I've asked a question on here. So forgive me if this is a stupid question, or it's not possible. I'm just at my wit's end trying to get this to work.
Nothing is wrong with your function. It's failing because of an undocumented restriction using the onEdit trigger to search DriveApp. If you run the script as is and then go to View > Execution transcript, you'll see the failure message at the bottom.
You can call the function successfully from a custom menu. Instead of watching for the edited row, use .getActiveCell(). It will search the string inside the selected cell on the spreadsheet. A working example is below.
function onOpen() {
SpreadsheetApp.getUi().createMenu("Search").addItem("Run", "searchFiles").addToUi();
}
function searchFiles() {
var string = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveCell().getValue();
var result = DriveApp.searchFiles('title contains "' + string + '"');
while (result.hasNext()) {
var file = result.next();
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().appendRow([file.getName()]);
}
}

Google Sites "addListItem" fails with Google Form Prefilled URL

I am working up a solution which returns a link to a prefilled Google Form, to be located on a classic Google Sites List Page. I have all the code in place, everything works, aside from the last vital part: programmatically, via Google Apps Script, adding the Google Form prefilled url as a new listitem on a Google Sites List Page. I am doing the prefilling using variables from a spreadsheet (the Responses from the google form). The url/anchor code looks perfectly formed in the Log. Here is the code:
//fetch all spreadsheet row entries to variables
var keyname = candname;
var keytimestamp = ActiveSheet.getRange("A"+ActiveRow).getValue();
var keytimestamp = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd");
var keyinc01 = ActiveSheet.getRange("B"+ActiveRow).getValue();
var keyinc02 = ActiveSheet.getRange("C"+ActiveRow).getValue();
var keyinc03 = ActiveSheet.getRange("D"+ActiveRow).getValue();
var keybrok01 = ActiveSheet.getRange("E"+ActiveRow).getValue();
var keybrok02 = ActiveSheet.getRange("F"+ActiveRow).getValue();
var keybrok03 = ActiveSheet.getRange("G"+ActiveRow).getValue();
var keyacc01 = ActiveSheet.getRange("H"+ActiveRow).getValue();
var keyacc02 = ActiveSheet.getRange("I"+ActiveRow).getValue();
var keyacc03 = ActiveSheet.getRange("J"+ActiveRow).getValue();
//var prefillurl = "https://www.google.co.uk"; //test other url
var prefillurl = 'https://docs.google.com/a/xxxxxxxx.org/forms/d/e/1FAIpQLSfOT0CVpSyGrlZmKKIRVCqbg11rAa9ANyYL8u9QwIWjqWfITg/viewform?entry.1894338678='+keyinc01+'&entry.1429410229='+keyinc02+'&entry.868549131='+keyinc03+'&entry.1083479546='+keybrok01+'&entry.1385475363='+keybrok02+'&entry.137722395='+keybrok03+'&entry.1074722805='+keyacc01+'&entry.1093081320='+keyacc02+'&entry.409101030='+keyacc03;
var listpageurl = "Google Form Link";
Logger.log(listpageurl);
var site = SitesApp.getSiteByUrl("https://sites.google.com/a/xxxxxxxx.org/xxxxxxxx");
var page = site.getChildByName("/home/candidates/"+candwebname);
page.addListItem([ keytimestamp, ssname, listpageurl ]);
As you can see I have tested the anchor tag syntax with the commented "https://www.google.co.uk", which works, and the prefilled url pasted into a browser also works correctly (grabbed from the Log). Also the listpage will accept the url via manual direct entry.However I always get the error about the anchor tag not being properly formed when I run the code. I have also tested with and without the https:// in the url, just in case. I have either missed something fundemental, have left out some additional quotes or double quotes, or have found a bug?
I have found a workaround, which was to enable UrlShortener, but would prefer to be able to use the prefilled url directly ( I will have a lot of these to do!) to speed things up, and make the code more transferrable.
Can anyone spot the issue here?