i use directionFinder in Google Apps Script application and I want do add some waypoints. Everything works well when i add this like this:
var directions = Maps.newDirectionFinder().setOrigin(start).setDestination(end)
.addWaypoint('Berlin')
.addWaypoint('Hamburg')
.getDirections();
but when I want do add some waypoints dynamicaly in loop like this:
var directions = Maps.newDirectionFinder().setOrigin(start).setDestination(end)
while (results.hasNext()) {
directions.addWaypoint('Berlin')
directions.addWaypoint('Hamm')
}
directions.getDirections();
finally the object directions hasn't any waypoints. What I'm doing wrong? There isn't possible to add waypoints in a loop?
I created a simple script (see bellow). It works correctly - the res contains 4 legs.
function testMapWayPoints() {
var directions = Maps.newDirectionFinder().setOrigin('Berlin').setDestination('Rotterdam');
var wayPoints = ['Potsdam', 'Hanover', 'Dortmund'];
for (var i = 0; i < wayPoints.length; i++) {
directions.addWaypoint(wayPoints[i]);
}
var res = directions.getDirections();
}
I assume that the results.hasNext() returns false. If not then, could you create a minimum compilable example/function which is possible to copy-and-paste to the editor and which reproduces the problem.
Update 00: Additionally I wrote a deployed web app demonstrating that the addWaypoint function works. Here is the app source code.
Related
I already have code that works, but the formatting is not preserved. When executing the function, a pop-up window is displayed, the words can be entered here. When the script has run through, the words are replaced but the formatting of the file is completely broken. I think it's because of getParagraphs. Does anyone know an alternative?
CODE:
function anpassen(){
var varMap={};
var file=DriveApp.getFileById(DocumentApp.getActiveDocument().getId());
var doc= DocumentApp.openById(file.getId());
var para= doc.getBody().getParagraphs();
for(var i=0; i<para.length;i++){
var text=para[i].getText();
if (!text) continue;
text=text.replace(/{{([^}]*)}}/g, function(m, $1) {
if(!varMap[$1]){varMap[$1]= DocumentApp.getUi().prompt($1+"?").getResponseText();
}
return varMap[$1];
});
para[i].setText(text);
}
}
Use Advanced Google Services
As specified in the documentation you can use Document API directly by using Google Apps Script, keep in mind that you'll need to import this Advanced Service like the following:
what's more there's an example of how to handle the style while adding a new text.
I need to store an Australian domain as a string. Australian domains end with ".com.au"
Google Scripts seems to be displaying all instances of ".com.au" with "(class)".
To reproduce, create a basic function in Google Apps Scripts as follows:
function myFunction() {
var x = "com.au";
console.log("x: " + x);
var z = 1; //<--create break point here
}
Create a breakpoint at var z = 1, and then debug the script.
Actual Results:
In the console (at breakpoint):
x: (class)
Expected Results:
x: "com.au"
*Note: upon further testing, from a practical perspective, "(class)" seems to still be treated as "com.au" so this is not a blocker to use, but it is odd and does not help when debugging.
This issue was reported as a bug to Google: https://issuetracker.google.com/issues/114358543
This looks to be a bug of the Google Apps Script editor debugger that should be reported to Google. Please follow the directions on https://developers.google.com/apps-script/support#bugs.
I just got around this bug with the use of an Array.
function myFunction() {
var array = new Array();
var text ="email#email.com.br";
array[0] = string;
}
text returns email#email.(class)
array[0] returns email#email.com.br
I am trying to build a complex string to insert a new formulae on the fly in google script. In VBA you use chr(34), is there a similar work around in google script?
Thanks
From this thread "Some common VBA functions translated to Google Apps Script" you will find here in Slide 6 that the function Chr(n) in VBA is translated in the javascript:
function Chr(n) {
return String.fromCharCode(n);
}
You can also find it in this Github.
For the sample code on how to use this function in AppsScript, check this link.
var map = Maps.newStaticMap();
for (var i = 0; i < route.legs.length; i++) {
var leg = route.legs[i];
if (i == 0) {
// Add a marker for the start location of the first leg only.
map.setMarkerStyle(markerSize, markerColor, String.fromCharCode(markerLetterCode));
map.addMarker(leg.start_location.lat, leg.start_location.lng);
markerLetterCode++;
}
map.setMarkerStyle(markerSize, markerColor, String.fromCharCode(markerLetterCode));
map.addMarker(leg.end_location.lat, leg.end_location.lng);
markerLetterCode++;
}
I am trying to display a Google Visualization GeoMap on my website. I created the code in the Code Playground and saved it as an Apps Script in my website. The page goes through the load process but nothing is displayed.
Here is the code:
function doGet() {
var app = UiApp.createApplication();
function drawVisualization() {
var query = new google.visualization.Query(
'https://docs.google.com/a/mantisnetworks.co/spreadsheet/ccc?key=0AoNHsySj5NxGdGt0dmxva3ZPb3dLYVpVZ2Z4TThNbGc&usp=drive_web#gid=0');
query.send(handleQueryResponse);
}
function handleQueryResponse(response){
if(response.isError()){
alert('Error in Query:' + response.getMessage()+''+response.getDetailedMessage());
return;
}
var data = response.getDataTable();
var geochart = new google.visualization.GeoMap(document.getElementById('visualization'));
var options = {};
options['dataMode'] = 'regions';
options['resolution'] = 'provinces';
options['region'] = 'LS';
options['width'] = '600px';
options['height'] = '300px';
geochart.draw(data, options);
}
app.close();
return app;
}
Google Apps Script is based on Javascript, but as a server-side environment it does not have access to all client-side javascript constructs. The Google visualizations, for instance, are provided as the Charts Service. Using that service, you'll find support for much of the visualization API. However, you won't find GeoMap.
The code you've provided in your question needs to be reworked considerably to work properly in Google Apps Script. Start with the example given on the Charts Service page, then adapt to your situation.
You do have another alternative within Google Apps Script, which is to use the HTML service to "host" an HTML page containing "real" javascript. Javascript that's embedded in HTML pages can be made to run on the client browser, so the example you cooked up in the playground should work. A full run-down of this option is beyond the scope of your question, but if you're interested in it you could start by scanning previous questions about the HTML Service.
I used the code given in
Embedding Google DocList in a Google Site
to embed a list of files (taken from a folder in Google drive) in a page of Google Sites. I like to know, if there's a way to modify the links on the output-page, so that they direct to the live-form of the spreadsheet and not to the spreadsheet-mode as it's set in the script.
EDIT : sorry about that, forget this first answer as there is actually a method to get form urls !!
here is how it works
function myFunction() {
var ss=SpreadsheetApp.getActive()
var formurl = ss.getFormUrl()
Browser.msgBox(formurl)
}
so it would be quite easy to embed a list in a site page,
here is a small code that does the job, online version here (needs authorization) be patient : can be slow to show up...
function doGet() {
var app=UiApp.createApplication().setTitle('Available Forms').setStyleAttribute('padding', '25');
var user = Session.getEffectiveUser().getUserLoginId();
app.add(app.createLabel('Available forms in Drive for '+user).setStyleAttribute('background', '#dddd33').setPixelSize(500, 18).setHorizontalAlignment(UiApp.HorizontalAlignment.CENTER).setStyleAttribute('fontWeight', 'bold'))
var flex=app.createFlexTable().setId('flex').setWidth('500').setBorderWidth(1).setCellPadding(2).setStyleAttribute('borderColor', '#dddd33');
var index = 0
var docs = DocsList.getAllFiles()
for (var n in docs){
if(docs[n].getFileType()=="spreadsheet"){
var ss=SpreadsheetApp.openById(docs[n].getId())
if(ss.getFormUrl()){
flex.setText(index, 0, docs[n].getName())
flex.setWidget(index, 1, app.createAnchor('open form', ss.getFormUrl()))
++ index
}
}
}
app.add(flex)
return app
}
again sorry that I forgot this feature.
-Forms are embedded in spreadsheet and don't appear as individual entities in your drive. Their ID (or URL) are not available from apps-script.... I'm afraid you'll have to encode the URLs manually.-
You could create a Document and name it the title of your Google Form. Stick the URL of the form inside the doc.