Adobe InDesign Javascript Error! Error number: 55 Error String: Object does not support the property or method 'documentPreferences' - javascript

I'm getting an error when I try to run the following code in InDesign.
I'm actually getting this code directly from an Adobe tutorial, not sure why they'd get their own code wrong. Any way you could help would be greatly appreciated! Thanks.
try{myFont = app.fonts.item("Arial");
}
catch (myError){};
var myDocument = app.documents.item(0);
with(myDocument){
var myPage = pages.item(0);
var myBounds = myGetBounds(myPage,myDocument);
with(myDocument.pages.item(0)){
//Get a reference to the text frame.
var myTextFrame = textFrames.item(0);
//Change the size of the text frame.
myTextFrame.geometricBounds = myBounds;
var myParagraph = myTextFrame.paragraphs.item(0);
myParagraph.appliedFont = myFont;
myParagraph.justification = Justification.centerAlign;
myParagraph.pointSize = 48;
}
}
//myGetBounds is a function that returns the bounds
//of the "live area" of a page.
function myGetBounds(myDocument, myPage){
var myPageWidth = myDocument.documentPreferences.pageWidth;
var myPageHeight = myDocument.documentPreferences.pageHeight;
if(myPage.side == PageSideOptions.leftHand){
var myX2 = myPage.marginPreferences.left;
var myX1 = myPage.marginPreferences.right;
}else{
var myX1 = myPage.marginPreferences.left;
var myX2 = myPage.marginPreferences.right;
}
var myY1 = myPage.marginPreferences.top;
var myX2 = myPageWidth - myX2;
var myY2 = myPageHeight - myPage.marginPreferences.bottom;
return [myY1, myX1, myY2, myX2];
}

It looks like in the myGetBounds() function you are passing the arguments in the wrong order.
In the function definition you can see that the document should be passed first and then the page second, but in the line starting with var myBounds you do it the other way around.
So the line should probably be:
var myBounds = myGetBounds(myDocument, myPage);

Related

If GoogleJsonResponseException: then skip and move to next row

I have a working script. need to improvise to have no manual interruption. We have multiple Profiles in Analytics, sometimes we lose access and sometimes we have. So when i run the Script, If we lost access to 1 of 60 profiles, i have to delete that entry manually then rerun the script.
What i want is, If there is below error, Then skip and continue with next row
"GoogleJsonResponseException: API call to analytics.data.ga.get failed with error: User does not have sufficient permissions for this profile."
function GoogleAnalytics() {
var doc2 = SpreadsheetApp.getActiveSpreadsheet();
var dashboard = doc2.getSheetByName("Dashboard");
for(var i=52;i<65;i++){
var viewId = dashboard.getRange(i,13).getValue(); // Your Google Analytics view ID
var metric = 'ga:metric, ga:metric2, ga:metric3';
var option = {'segment': 'gaid::-5'};
var result = Analytics.Data.Ga.get(viewId, metric, option);
var metric = result.totalsForAllResults['ga:metric'];
var metric2 = result.totalsForAllResults['ga:metric2'];
var metric3 = result.totalsForAllResults['ga:metric3'];
var doc = SpreadsheetApp.getActiveSpreadsheet(); // Current document
var sheet = doc.getActiveSheet(); // Current sheet
sheet.getRange(i,14,1,1).setValue(metric);
sheet.getRange(i,15,1,1).setValue(metric2);
sheet.getRange(i,16,1,1).setValue(metric3);
} }
try it this way:
function GoogleAnalytics() {
var doc2 = SpreadsheetApp.getActiveSpreadsheet();
var sh = doc2.getSheetByName("Dashboard");
var sheet = doc2.getActiveSheet(); // Current sheet
const vs = sh.getRange(52, 13, 13).getValues();
var metric = 'ga:metric, ga:metric2, ga:metric3';
var option = { 'segment': 'gaid::-5' };
for (var i = 0; i < vs.length; i++) {
var viewId = vs[i][0]; // Your Google Analytics view ID
try {
var result = Analytics.Data.Ga.get(viewId, metric, option);
}
catch(e){
continue;
}
if (result) {
sheet.getRange(i + 52, 14, 1, 3).setValues([[result.totalsForAllResults['ga:metric'], result.totalsForAllResults['ga:metric2'], result.totalsForAllResults['ga:metric3']]]);
}
}
}
Without the benefit of working data some of this may not be correct but using setValues and getValues should speed it up considerably and the try catch blocks should help with not getting result consistently. Also you want to avoid making unnecessary declarations in loops.
I might understand the question incorrectly (if so, please clarify) but it sounds to me like you just need to add...
function GoogleAnalytics() {
var doc2 = SpreadsheetApp.getActiveSpreadsheet();
var dashboard = doc2.getSheetByName("Dashboard");
for(var i=52;i<65;i++){
try { //...this line and...
var viewId = dashboard.getRange(i,13).getValue(); // Your Google Analytics view ID
var metric = 'ga:metric, ga:metric2, ga:metric3';
var option = {'segment': 'gaid::-5'};
var result = Analytics.Data.Ga.get(viewId, metric, option);
var metric = result.totalsForAllResults['ga:metric'];
var metric2 = result.totalsForAllResults['ga:metric2'];
var metric3 = result.totalsForAllResults['ga:metric3'];
var doc = SpreadsheetApp.getActiveSpreadsheet(); // Current document
var sheet = doc.getActiveSheet(); // Current sheet
sheet.getRange(i,14,1,1).setValue(metric);
sheet.getRange(i,15,1,1).setValue(metric2);
sheet.getRange(i,16,1,1).setValue(metric3);
} catch(e) { //...this part
console.log(e); //optional, catch(e){} is perfectly valid as well, or any code you might want to execute on error
}
} }

"It is necessary to detach the element" error in google docs script

When I try to copy paragraphs form one doc to another I get unexpected error:
It is necessary to detach the element
What does it mean? What am I doing wrong?
function test_copy_paragrahps() {
var final = 'final';
var doc1 = get_doc('', final);
var doc2 = create_doc_in_path('', final+'test');
var body1 = doc1.getBody();
var body2 = doc2.getBody();
var par1 = body1.getParagraphs();
for (var i=0;i<par1.length;i++) {
body2.insertParagraph(i, par1[i]);
}
}
here is video http://youtu.be/1WdCD5ATiYw
P.S. You can not mention on get_doc and create_doc_in_path implementations. Both return Document object.
You attempted to insert a paragraph that already has a parent Body. You need to create a detached copy of the paragraph before you can insert it.
See this part of the documentation that mentions detaching a paragraph.
I believe this will fix the error:
function test_copy_paragrahps() {
var final = 'final';
var doc1 = get_doc('', final);
var doc2 = create_doc_in_path('', final+'test');
var body1 = doc1.getBody();
var body2 = doc2.getBody();
var par1 = body1.getParagraphs();
for (var i=0;i<par1.length;i++) {
body2.insertParagraph(i, par1[i].copy()); //--- copy()
}
}

Why won't my Google Calendar Script run?

I am using Google Script to create a form that will create an event, as you can see from the code I have used two panels. I want to create multiple event forms on the 'parent' panel, I am trying to use the 'child' to hold information about the event. I want to run createEvent() on each child panel, but when I run it the script will not run and show this error message:
"Error occured: InternalError: Cannot find method createEvent((class),(class),(class),object)."
function doGet(){
var app = UiApp.createApplication().setTitle('Create Events');
//Create a penel which holds all the form elelemnts
var parent = app.createHorizontalPanel().setId('parent');
var panelOne = app.createVerticalPanel().setBorderWidth(2);
var eventTitleLabel = app.createLabel('Event Title:');
var eventTitle = app.createTextBox();
var eventStartDateLabel = app.createLabel('Event Start Date:');
var evenStartDate = app.createDateBox();
var eventEndDateLabel = app.createLabel('Event End Date:');
var evenEndDate = app.createDateBox();
var eventDeatilLabel = app.createLabel('Event Details:');
var eventDetail = app.createTextArea().setSize('150', '100');
var eventButton = app.createButton('Create Events');
var cancelButton = app.createButton('Cancel');
panelOne.add(eventTitleLabel)
.add(eventTitle)
.add(eventStartDateLabel)
.add(evenStartDate)
.add(eventEndDateLabel)
.add(evenEndDate)
.add(eventDeatilLabel)
.add(eventDetail)
.add(eventButton)
.add(cancelButton);
var eventHandler = app.createServerClickHandler('createEvents');
eventHandler.addCallbackElement(parent);
//Add this handler to the button
eventButton.addClickHandler(eventHandler);
parent.add(panelOne);
app.add(parent);
app.close();
return app;
}
function createEvents(e){
//Get the active application
var app = UiApp.getActiveApplication();
try{
var eventTitle = e.parameter.eventTitle;
var eventStartDate = e.parameter.eventStartDate;
var eventEndDate = e.parameter.eventEndDate;
var eventDetails = e.parameter.eventDetail;
var cal = CalendarApp.getDefaultCalendar();
cal.createEvent(eventTitle, eventStartDate,eventEndDate,{description:eventDetails});
app.add(app.createLabel('Event created Successfully'));
app.getElementById('panel').setVisible(false);
return app;
}
catch(e){
app.add(app.createLabel('Error occured: '+e));
return app;
}
}
All the input widgets need a .setName() to be able to pass their values to the server function. https://developers.google.com/apps-script/guides/ui-service#ServerHandlers
Also consider using Logger.log() for debugging.
You simply forgot to set names to the widgets you want to get data from... in your example :
var eventTitleLabel = app.createLabel('Event Title:');
var eventTitle = app.createTextBox().setName('eventTitle');
var eventStartDateLabel = app.createLabel('Event Start Date:');
var evenStartDate = app.createDateBox().setName('evenStartDate');
var eventEndDateLabel = app.createLabel('Event End Date:');
var evenEndDate = app.createDateBox().setName('evenEndDate');
var eventDeatilLabel = app.createLabel('Event Details:');
var eventDetail = app.createTextArea().setSize('150', '100').setName('eventDetail');

How to check table for new content, and then update browser tab title?

I am working on a way to flash a browser tab when a new message appears in a table. I have the flashing of the tab part working, my only problem is that I can't seem to get it to flash when a message is received (which is the whole point of my exercise :) )
The newMessage() function is working fine, I just can't seem to get the notification() function to work.
My code is as follows:
function newMessage()
{
var oldTitle = "Your Page";
var msg = "New Message";
var timeout = setInterval(function()
{
document.title = document.title == msg ? '' : msg;
}, 1000);
window.onmousemove = function() {
clearInterval(timeout);
document.title = oldTitle;
window.onmousemove = null;
};
}
function notification()
{
var index = 2;
var content = document.getElementById('refreshMessages').childNodes[index];
var content = document.getElementById('refreshMessages').getElementByTagName("tr")[1];
var knownContent = content.toString();
updater.start();
updater2.start();
var newContent = document.getElementById('refreshMessages').childNodes[index];
var newContent = document.getElementById('refreshMessages').getElementByTagName("tr")[1];
if(knownContent != newContent.toString())
{
newMessage();
knownContent = newContent;
}
else if(knownContent = newContent.toString())
{
alert("No need to flash title.");
}
}
notification();
In the notification() function, I am trying to call the newMessage() function by comparing the strings at the appropiate cell in the table.
I put the alert() into the else if just to see if it would be called, but it does not happen. update.start() and update2.start() are carried out however, as I can see the messages appearing in the table.
I would be happier to use JavaScript but I am open to jQuery also.
My JavaScript is very very rusty so excuse me if I have made any silly mistakes!
Thanks,
Chuck
You have several mistakes in function notification(), see my comments:
function notification()
{
var index = 2;
//Why are you assigning value to "content" for twice?
var content = document.getElementById('refreshMessages').childNodes[index];
/*
* function getElementByTagName is undefined, should be getElementsByTagName,
* 's' is missing. And [1] means the second one not the first one, make sure
* that's exactly what you want.
*/
var content = document.getElementById('refreshMessages').getElementByTagName("tr")[1];
/*
* content is a tr dom object, content.toString() is something like "[object]".
* If you want to get content inside a cell, you should use cell.innerHTML.
* e.g. A table:
* <table id="refreshMessages">
* <tr><td>Hello world</td></tr>
* </table>
* var table = document.getElementById('refreshMessages');
* var firstTr = table.getElementsByTagName("tr")[0];
* var firstTd = firstTr.getElementsByTagName("td")[0];
* alert(firstTd.innerHTML); //alerts "Hello world"
*/
var knownContent = content.toString();
//I doubt these functions really get invoked cuz there's javascript error above.
updater.start();
updater2.start();
//assigning twice, "getElementByTagName" is missing "s"
var newContent = document.getElementById('refreshMessages').childNodes[index];
var newContent = document.getElementById('refreshMessages').getElementByTagName("tr")[1];
//Remove toString(), use innerHTML i metioned above.
if(knownContent != newContent.toString())
{
newMessage();
knownContent = newContent;
}
//You miss an "=" here, to judge a equals b, you should use "=="
else if(knownContent = newContent.toString())
{
alert("No need to flash title.");
}
}

pass argument to mouseCallback

I have a simple, but problematic question.
Is there a way, using google script to send an argument to a mouseCallback?
Some examples are explained here, but each time the cell in written in the function.
I want this cell to change over time, and I need to pass a string to the callback.
This works
function foo() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication();
var button = app.createButton('submit');
app.add(button);
var handler = app.createServerClickHandler('b');
button.addClickHandler(handler);
doc.show(app);
}
function b() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var cell = doc.getRange('a1');
cell.setValue(Number(cell.getValue()) + 1);
var app = UiApp.getActiveApplication();
app.close();
// The following line is REQUIRED for the widget to actually close.
return app;
}
I would like something like this
function foo() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication();
var button = app.createButton('submit');
app.add(button);
var value = 'A1';
var handler = app.createServerClickHandler('b', value);
button.addClickHandler(handler);
doc.show(app);
}
function b(value) {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var cell = doc.getRange(value);
cell.setValue(Number(cell.getValue()) + 1);
var app = UiApp.getActiveApplication();
app.close();
// The following line is REQUIRED for the widget to actually close.
return app;
}
Can anyone help me in this?
Thanks !
There is answers to this question in here :
Thansk to google forum users :)
http://www.google.com/support/forum/p/apps-script/thread?fid=2ef3693fc9aee1050004ad903de88dd4&hl=en

Categories