i need to get the page name of a google sites with google apps script, there is another post about it but is about URL such as Retrieve page title from URL in Apps Script, but i need to get the page title name.
Thanks.
If you are in a domain, use the following, replacing teh strings as appropriate.
var site = SitesApp.getSite("mydomainname.org", "sitename");
var page = site.getChildren()[0];
var title = page.getTitle();
Karl
As per this stackoverflow answer, the function works well for getting page title.
function betweenMarkers(text, begin, end) {
var firstChar = text.indexOf(begin) + begin.length;
var lastChar = text.indexOf(end);
var newText = text.substring(firstChar, lastChar);
return newText;
}
var pageTitle = betweenMarkers(response.getContentText(),"<title>","</title>")
console.log(pageTitle)
Related
I have the following issue. I am trying to create a script that will autofill a template google document using the submission of a google form. I am able to get the script to work for questions that are input with text but am struggling on getting the data from questions in the form that are checkboxes (or multiple choice) to work and fill the google document. Any assistance would be great. For example the variable identified as "offense" is from a question with checkboxes that has about 30 different options, I would like each option that is checked on the form to replace text within my google doc. Thanks.
function autoFillGoogleDocFromForm(e) {
//e.values is an array of form values
var timestamp = e.values[4];
var studentName = e.values[3];
var oe = e.values[16];
var gradelevel = e.values[14];
var program = e.values[15];
var offense = e.values[6];
var action = e.values[18];
var serve = e.values[31];
var makeUp = e.values[32];
var comments = e.values[29];
//file is the template file, and you get it by ID
var file = DriveApp.getFileById('1nPWC0IKc1zUJXYxbGahJsSW4uNWwhxnLM8shcD8kEE4');
//We can make a copy of the template, name it, and optionally tell it what folder to live in
//file.makeCopy will return a Google Drive file object
var folder = DriveApp.getFolderById('1FlpHRKqYrEHttA-3ozU3oUVJlgiqqa-F')
var copy = file.makeCopy(studentName + ', ' + timestamp, folder);
//Once we've got the new file created, we need to open it as a document by using its ID
var doc = DocumentApp.openById(copy.getId());
//Since everything we need to change is in the body, we need to get that
var body = doc.getBody();
//Then we call all of our replaceText methods
body.replaceText('<<Student Name>>', studentName);
body.replaceText('<<Incident Date>>', timestamp);
body.replaceText('<<Student Grade>>', gradelevel);
body.replaceText('<<Open enrolled?>>', oe);
body.replaceText('<<IEP/504?>>', program);
body.replaceText('<<Reason for Referral (Handbook)>>', offense);
body.replaceText('<<Administrative Action>>', action);
body.replaceText('<<Date(s) to be Served>>', serve);
body.replaceText('<<Make up Date(s)>>', makeUp);
body.replaceText('<<Comments>>', comments);
//Lastly we save and close the document to persist our changes
doc.saveAndClose();
}
You need to use the labels assigned to the checkboxes to determine if they have been checked. Same for multiple coice.
You can't use ListItems because you can't set the glyph to a check box so I simply insert text with a checkbox character.
I created a form
I then created an onFormSubmit(e) installed trigger in the spreadsheet to get the form response and put it in the Doc. Here I've simply used an active doc to perform my tests. You will need to adjust the script to handle your template doc.
function onFormSubmit() {
// test data
let e = {"authMode":"FULL","namedValues":{"Timestamp":["8/16/2022 14:40:26"],"Student Grade":["Junior"],"Reason for Referrel":["Bad grades, Disruptive in class, Other"],"Student Name":["Joe Smith"],"Open Enrollment":["Yes"]},"range":{"columnEnd":5,"columnStart":1,"rowEnd":2,"rowStart":2},"source":{},"triggerUid":"12151926","values":["8/16/2022 14:40:26","Joe Smith","Junior","Yes","Bad grades, Disruptive in class, Other"]};
try {
let doc = DocumentApp.getActiveDocument();
let body = doc.getBody();
let referrels = ["Bad grades","Unexcused absence","Disruptive in class","Fighting","Other"];
body.replaceText("<<Student Name>>",e.namedValues["Student Name"]);
body.replaceText("<<Student Grade>>",e.namedValues["Student Grade"]);
body.replaceText("<<Open Enrollment>>",e.namedValues["Open Enrollment"]);
// Notice the regex expression below because findText doesn't seem to handle parenthesis well
let text = body.findText("<<Reason for Referral.*>>");
body.replaceText("<<Reason for Referral.*>>","");
if( text ) {
let index = body.getChildIndex(text.getElement().getParent())+1;
referrels.forEach( item => {
let checked = e.namedValues["Reason for Referrel"][0].indexOf(item);
if( checked >= 0 ) {
let listItem = body.insertListItem(index,item);
index = body.getChildIndex(listItem)+1;
}
}
);
}
}
catch(err) {
Logger.log(err);
}
}
I have the following issue. I am trying to create a script that will autofill a template google document using the submission of a google form. I am able to get the script to work for questions that are input with text but am struggling on getting the data from questions in the form that are checkboxes (or multiple choice) to work and fill the google document. Any assistance would be great. For example the variable identified as "offense" is from a question with checkboxes that has about 30 different options, I would like each option that is checked on the form to replace text within my google doc. Thanks.
function autoFillGoogleDocFromForm(e) {
//e.values is an array of form values
var timestamp = e.values[4];
var studentName = e.values[3];
var oe = e.values[16];
var gradelevel = e.values[14];
var program = e.values[15];
var offense = e.values[6];
var action = e.values[18];
var serve = e.values[31];
var makeUp = e.values[32];
var comments = e.values[29];
//file is the template file, and you get it by ID
var file = DriveApp.getFileById('1nPWC0IKc1zUJXYxbGahJsSW4uNWwhxnLM8shcD8kEE4');
//We can make a copy of the template, name it, and optionally tell it what folder to live in
//file.makeCopy will return a Google Drive file object
var folder = DriveApp.getFolderById('1FlpHRKqYrEHttA-3ozU3oUVJlgiqqa-F')
var copy = file.makeCopy(studentName + ', ' + timestamp, folder);
//Once we've got the new file created, we need to open it as a document by using its ID
var doc = DocumentApp.openById(copy.getId());
//Since everything we need to change is in the body, we need to get that
var body = doc.getBody();
//Then we call all of our replaceText methods
body.replaceText('<<Student Name>>', studentName);
body.replaceText('<<Incident Date>>', timestamp);
body.replaceText('<<Student Grade>>', gradelevel);
body.replaceText('<<Open enrolled?>>', oe);
body.replaceText('<<IEP/504?>>', program);
body.replaceText('<<Reason for Referral (Handbook)>>', offense);
body.replaceText('<<Administrative Action>>', action);
body.replaceText('<<Date(s) to be Served>>', serve);
body.replaceText('<<Make up Date(s)>>', makeUp);
body.replaceText('<<Comments>>', comments);
//Lastly we save and close the document to persist our changes
doc.saveAndClose();
}
You need to use the labels assigned to the checkboxes to determine if they have been checked. Same for multiple coice.
You can't use ListItems because you can't set the glyph to a check box so I simply insert text with a checkbox character.
I created a form
I then created an onFormSubmit(e) installed trigger in the spreadsheet to get the form response and put it in the Doc. Here I've simply used an active doc to perform my tests. You will need to adjust the script to handle your template doc.
function onFormSubmit() {
// test data
let e = {"authMode":"FULL","namedValues":{"Timestamp":["8/16/2022 14:40:26"],"Student Grade":["Junior"],"Reason for Referrel":["Bad grades, Disruptive in class, Other"],"Student Name":["Joe Smith"],"Open Enrollment":["Yes"]},"range":{"columnEnd":5,"columnStart":1,"rowEnd":2,"rowStart":2},"source":{},"triggerUid":"12151926","values":["8/16/2022 14:40:26","Joe Smith","Junior","Yes","Bad grades, Disruptive in class, Other"]};
try {
let doc = DocumentApp.getActiveDocument();
let body = doc.getBody();
let referrels = ["Bad grades","Unexcused absence","Disruptive in class","Fighting","Other"];
body.replaceText("<<Student Name>>",e.namedValues["Student Name"]);
body.replaceText("<<Student Grade>>",e.namedValues["Student Grade"]);
body.replaceText("<<Open Enrollment>>",e.namedValues["Open Enrollment"]);
// Notice the regex expression below because findText doesn't seem to handle parenthesis well
let text = body.findText("<<Reason for Referral.*>>");
body.replaceText("<<Reason for Referral.*>>","");
if( text ) {
let index = body.getChildIndex(text.getElement().getParent())+1;
referrels.forEach( item => {
let checked = e.namedValues["Reason for Referrel"][0].indexOf(item);
if( checked >= 0 ) {
let listItem = body.insertListItem(index,item);
index = body.getChildIndex(listItem)+1;
}
}
);
}
}
catch(err) {
Logger.log(err);
}
}
I want to show a preview of a file (located on Google Drive) selected from a list or tree.
If I try to display an image file the image doesn't show up (allthough it is the right fileId)
function doGet()
{
var app = UiApp.createApplication().setTitle("Image");
var urlDrive = 'https://drive.google.com/file/d/0BxjtiwHnjnkrUVFKaWVaM3BNZjg';
var urlWeb = 'http://cdn.ndtv.com/tech/images/gadgets/google_webfonts_ap.jpg';
// var url = urlWeb; // works
var url = urlDrive; // Doe NOT work
var image = app.createImage(url).setHeight(200);
var panel = app.createVerticalPanel();
panel.add(image);
app.add(panel)
return app;
}
The example shows that changing the url to a file not present on Google Drive it works.
In general I would like to know if it is possible to preview a file (including msWord, msExcel and pdf) in a panel using GAS. A small example will be appreciated much of course.
The problem is the way you get the url.
The only url that works in this case is the one you can see when you examine the image file with "details" in your drive browser window.
it is actually build differently but contains the ID so it is quite easy to re-build...
Here is how it goes:
function doGet(){
var app = UiApp.createApplication().setTitle("Image");
var imgFile = DriveApp.getFilesByName('Chrysanthemum.jpg').next()
ID = imgFile.getId();
Logger.log(ID);
var imgUrl = 'https://googledrive.com/host/'+ID
var image = app.createImage(imgUrl).setHeight(200);
var panel = app.createVerticalPanel();
panel.add(image);
app.add(panel)
return app;
}
test here
EDIT :
Since the code above seems not to work for files that are not publicly shared (not sure) here is another way to get the result (test app updated) that works with files that are shared to 'anyone with the link can view'.
Note That this is not logical since the app is deployed to run "as me" and is accessible to anyone even anonymous"... so from the G Drive pov I open the file... go figure why it behave like that ;-)
I tested in an anonymous session and it works.
I hope this solution will be suitable for you.
function doGet(){
var app = UiApp.createApplication().setTitle("Image");
var imgFile = DriveApp.getFilesByName('Chrysanthemum.jpg').next()
var ID = imgFile.getId();
Logger.log(ID);
var imgUrl = 'https://drive.google.com/uc?export=view&id='+ID
// var imgUrl = 'https://googledrive.com/host/'+ID; //for public files apparently
var image = app.createImage(imgUrl).setHeight(200);
var panel = app.createVerticalPanel();
panel.add(image);
app.add(panel)
return app;
}
So I am writing a script which look at review done on google+ pages
and updates a google spreadsheet.
I have found out that the line in the html which holds this value is
<span class="A7a">103</span>
I just need to make it possible for me to extract from the page with just knowing the URL and that html code.
Use
var response = UrlFetchApp.fetch("http://www.website.com/");
To fetch the html of the page. Then use something like
var cut = response.substring( str.indexOf( "<span class="A7a">" ), response.length);
var value = cut.substring(0, cut.indexOf("</span>"));
https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String)
I agree with the previous answer but there are some omissions
var response = UrlFetchApp.fetch(url);
var str = response.getContentText();
var balise = '<span class="A7a">'
var cut = str.substring(str.indexOf( balise ), response.length);
var value = cut.substring(balise.length, cut.indexOf("</span>"));
I'm looking to get the contents of a Google word document and put it in a textBox. The following code is spitting out an error:
function listBoxClick(e) {
var tapp = UiApp.getActiveApplication();
var docName = DocsList.find(e.parameter.doclist); //find the document with the same name as what the user clicked on
var docContents = docName[0].getContentAsString(); //get contents of document
tapp.getElementById("songboxID").setValue(songfile2); //set the value of the textBox to the contents of the document
return tapp;
}
This returns the following error:
Unsupported conversion requested.
I read somewhere that we can't do this for Google Documents but we can for other non-google documents that we upload. Is that right?
Here's the answer that I can't post for 5 more hours since I'm new and have no reputation:
With Serge's assistance, here's what worked for me:
function listBoxClick(e) {
var tapp = UiApp.getActiveApplication();
var docName = DocsList.find(e.parameter.doclist); //get document name based on what user clicked on in listBox
var docId = docName[0].getId(); //get document ID
var content = DocumentApp.openById(docId).getText(); //get contents of document
tapp.getElementById("songboxID").setValue(content); //set web app's textBox (called songboxID) to match the contents of the document the user clicked on
return tapp;
}
You have to use DocumentApp.getText(); to get the text content of your document.
in you code it would become :
function listBoxClick(e) {
var tapp = UiApp.getActiveApplication();
Logger.log(e.parameter.doclist)
var docName = DocsList.find(e.parameter.doclist); //find the document with the same name as what the user clicked on
var docId = docName[0].getId();
Logger.log(docName[0].getName()+' = '+docId)
var textContent = DocumentApp.openById(docId).getText();
Logger.log(textContent)
tapp.getElementById("songboxID").setValue(textContent); //set the value of the textBox to the contents of the document
return tapp;
}
EDIT : as you noticed with your experiments DocsList.find(e.parameter.doclist) returns results that you didn't expect... that's simply because the find operator searches not only on doc's names but also on docs content.
To solve that , you should add a smal routine that checks the results of the find query against the documents name, something like this :
var docName = DocsList.find(e.parameter.doclist); //find the document with the same name as what the user clicked on
for(var d in docName){{
if(docName[d].getName()==e.parameter.doclist){
var docId = docName[d].getId();
}
}
This will ensure that the document you are looking for is actually the right one.
PS: sorry for not having mentioned that immediately... it just slipped out of my mind ;-)