Append drawing from one doc to another - google-apps-script

I want to create a function that copies all the content from one Google doc to another one. The template has tables, images, text and drawings The text and tables copy over just fine, but it's the drawings and pictures that don't seem to work. I've tried different things, but I always get errors.
This is what I have (I also got this from stackoverflow):
function copyTemplate() {
var thisDoc = DocumentApp.getActiveDocument();
var thisBody = thisDoc.getBody();
var templateDoc = DocumentApp.openById('LMwo6kT1_XDCh-8HkwTNN890W3_MeL6AJKU');
var templateBody = templateDoc.getBody();
for(var i=0; i<templateBody.getNumChildren();i++){
switch (templateBody.getChild(i).getType()) {
case DocumentApp.ElementType.PARAGRAPH:
thisBody.appendParagraph(templateBody.getChild(i).copy());
break;
case DocumentApp.ElementType.LIST_ITEM:
thisBody.appendListItem(templateBody.getChild(i).copy());
break;
case DocumentApp.ElementType.TABLE:
thisBody.appendTable(templateBody.getChild(i).copy());
break;
case DocumentApp.ElementType.INLINE_DRAWING:
var drawing = element.asParagraph();
thisBody.appendParagraph(templateBody.getChild(i).copy());
break;
case DocumentApp.ElementType.INLINE_IMAGE:
thisBody.appendImage(templateBody.getChild(i).copy());
break;
}
}
I'm a newby so maybe the answer is super easy but I don't seem to find it. Thanks so much in advance!

This worked for me.
case DocumentApp.ElementType.INLINE_DRAWING:
thisBody.appendImage(templateBody.getChild(i).copy());
break;
case DocumentApp.ElementType.INLINE_IMAGE:
thisBody.appendImage(templateBody.getChild(i).copy());
break;

Related

Double switch case not working properly. Google maps shapes drawing

I would like to create popup menu which includes: choosing maptypes, choosing shape with citywalls drawn on map.
My problems:
I have no idea how to clean once picked shape (or switch it to none, method doesnt matter)
How to make my map not to be loaded again when i change shape.
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(item.getItemId()){
case R.id.mediewal:
shape = new PolygonOptions();
mMap.addPolygon(shape);
break;
case R.id.swedish:
shape = new PolygonOptions()
.add(new LatLng(53.426685, 14.565516),
new LatLng(53.427519, 14.565268),
................................
new LatLng(53.427003, 14.565636))
.strokeColor(0xB3A60307)
.fillColor(0xB3670000)
.strokeWidth(8)
.geodesic(true);
mMap.addPolygon(shape);
shape = new PolygonOptions()
.add(new LatLng(53.420158, 14.557578),
new LatLng(53.420285, 14.557832),
................................
new LatLng(53.419972, 14.557818))
.strokeColor(0xB3A60307)
.fillColor(0xB3670000)
.strokeWidth(8)
.geodesic(true);
mMap.addPolygon(shape);
break;
case R.id.prussian:
shape = new PolygonOptions();
mMap.addPolygon(shape);
break;
case R.id.none:
shape = new PolygonOptions();
mMap.addPolygon(shape);
break;
}
switch(item.getItemId()){
case R.id.normal_map:
mapType = GoogleMap.MAP_TYPE_NORMAL;
break;
case R.id.satellite_map:
mapType = GoogleMap.MAP_TYPE_SATELLITE;
break;
case R.id.terrain_map:
mapType = GoogleMap.MAP_TYPE_TERRAIN;
break;
case R.id.hybrid_map:
mapType = GoogleMap.MAP_TYPE_HYBRID;
break;
}
mMap.setMapType(mapType);
return super.onOptionsItemSelected(item);
}
Of course Ive put only one test shape here. Currently its working like this: I can pick maptype and pick shape, but no way to clean shape in another way than going out to menu and go maps again.
Any help would be greatly appreciated
Generaly to avoid loading map over and over again I've moved mMap.setMapType(mapType);under every map type in switch case with mapTypes. Removed ofc this one from Return call.
To clen up shapes I call mMap.clear() which deletes all shapes and markers from map. Its not perfect solution for everybody, but for my case it's enough.
I think to clen up one shape we should call (with checkbox):
if(item.isChecked()){
item.setChecked(false);
polygon2.remove()
;}
else{
item.setChecked(true);
polygon2 = mMap.addPolygon(shape2);
}
break;
Remember to initialize shape e.g. private Polygon polygon2;

HTML5 CreateJS game, update function not working

Everything seems to work up to this function "update". A blue paddle (rectangle)should be appearing at the bottom right corner of my stage yet it's not.
const ARROW_KEY_LEFT = 37;
const ARROW_KEY_UP = 38;
var stage,padle;
var leftKeyDown,rightKeyDown = false;
function init(){
stage = new
createjs.Stage(document.getElementById("canvas"));
createjs.Ticker.addEventListener("tick", tick);
createjs.Ticker.setFPS(60);
startGame();
};
function startGame(){
padle = new createjs.Shape();
padle.width = 100;
padle.graphics.beginFill("#0000FF").drawRect(0, 0, padle.width, 20);
padle.x = padle.nextX = 0;
padle.y = stage.canvas.height - 20;
stage.addChild(padle);
window.onkeydown = movePadle;
window.onkeyup = stopPadle;
};
function movePadle(e){
e = !e ? window.event : e;
switch (e.keyCode){
case ARROW_KEY_LEFT:
leftKeyDown = true;
break;
case ARROW_KEY_RIGHT:
rightKeyDown = true;
break;
}
};
function stopPadle(e){
e = !e ? window.event : e;
switch (e.keyCode){
case 37:
leftKeyDown = false;
break;
case 39:
rightKeyDown = false;
break;
}
};
This is the update function where it doesn't seem to be functioning. Any help is appreciated!
function update(){
var nextX = padle.x;
if (leftKeyDown){
nextX = padle.x - 10;
if(nextX < 0){
nextX = 0;
}
}else if (rightKeyDown){
nextX = padle.x + 10;
if(nextX > stage.canvas.width - padle.width){
alert("right key down");
nextX = stage.canvas.width - padle.width;
}
}
function tick(e){
update();
render();
stage.update();
};
http://jsfiddle.net/9nff5kta/
And it's no longer working on jsfiddle
I played with this a little, and got it "working". JSFiddle is not the best place to test Flash HTML/JS output because of the way the export works with CreateJS. I made some small changes, which allows it to work in jsfiddle.
I put the JS imports into resources so they load first
I removed the "createjs" declaration in the JavaScript. You are including the keyboardEvents.js in the jsfiddle "JavaScript" window, which includes it inside of a closure , instead of globally, so it recreates the "createjs" namespace - which makes it unable to find the CreateJS libs in your class. By removing it, it forces it to use the loaded JS libs.
http://jsfiddle.net/lannymcnie/9nff5kta/1/
I believe this is what you would have started with locally.
Once this was working, I got an error in the console that ARROW_KEY_RIGHT was undefined. Your code only had ARROW_KEY_LEFT and ARROW_KEY_UP. Once I changed the variable name, it seems to work. Check out the modified fiddle above.
Note that I just opened the Dev tools in Chrome, and the error was caught immediately, so it was clear what was happening. The jsfiddle stuff was a little harder to debug.
Cheers.

Using createjs, preoloading and caching, why is my canvas still so slow?

I am trying to create a small RPG for class. I create one Bitmap image, cache it, and then every time I need that same Bitmap, I clone it, and finally add them to the stage. However, despite my efforts, my canvas is still extremely slow since I am drawing all of these bushes.
I would put them in a container, however, I need to know the X and Y position that way I know if the player is trying to step over their boundaries.
Here is my function:
parseRoom: function(mapObject, room, obj, image){
var letter = 'X';
//var object = obj;
var object = null;
var img = new createjs.Bitmap(image);
for(var m=0; m < mapObject.length; m++){
for(var j=0; j< mapObject[m].length; j++){
letter = mapObject[j][m];
switch (letter){
case 'X':
//do nothing
break;
case 'O':
//object = this.createObject();
//object.image = img.clone();
img.cache();
object = img.clone();
room.AddObstacle(object, m, j);
break;
}
}
}
}
and this is my function when I actually add them to the stage:
addObstacle: function(imgObj, x, y){
imgObj.x = x ||imgObj.x || 0;
imgObj.y = y ||imgObj.y || 0;
imgObj.setVisible = function(visible){
this.visible = visible;
};
imgObj.update = function(){
if(this.visible)this.visible= true;
else this.visible = false;
};
objects.push(imgObj);
stage.addChild(imgObj);
},
as you can see, I extend the Bitmap class and also add and update and a setVisible method to it. That way I can turn them all off or on depending on the screen. Any ideas that could help me and make my game smoother? Thank you!
I was constantly updating every object, including the bushes, and there was no need to update the bushes every tick. I simply removed logic from checking the bushes and now it runs really fast.

why is this script in as3 not working??

Hi I made this code and I use flash cs5.5
var cijfer_txt:int = parseInt(textarea_text.text);
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
submit.addEventListener(TouchEvent.TOUCH_TAP, fl_TapHandler_2);
function fl_TapHandler_2(event:TouchEvent):void
{
switch (cijfer_txt){
case 1:
gotoAndStop(12);
break;
case 2:
gotoAndStop(23);
break; }
};
but I don't get it why it isn't working, the animation has to go to frame 12 when I fill in "1" and stop and has to got to frame 23 if I fill in "2" and stop but he doesn't do it and I get sick of it!!
Try to set cijfer_txt in fl_TapHandler_2
function fl_TapHandler_2(event:TouchEvent):void
{
cijfer_txt = parseInt(textarea_text.text);
switch (cijfer_txt){
}
}
I think you have problem with debugging so I'll help you:
First: change your code as follows -
var cijfer_txt:int;
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
submit.addEventListener(TouchEvent.TOUCH_TAP, fl_TapHandler_2);
function fl_TapHandler_2(event:TouchEvent):void
{
cijfer_txt = parseInt(textarea_text.text);
trace("in the function, cijfer_txt = "+cijfer_txt);
switch (cijfer_txt) {
case 1:
trace("in case 1");
gotoAndStop(12);
break;
case 2:
trace("in case 2");
gotoAndStop(23);
break;
default:
trace("in defaukt");
break;
}
}
NOW RUN IT AND WATCH THE CONSOLE\OUTPUT FOR TRACE OUTPUTS,
according to the output you can see what is hapening!
trace(); is a very common method
Good Luck
(Don't forget to mark as accepted if it helped you!)

locate an object after tween

i want to create kind of slot machine.
i googled and found a sample code for that.
import com.greensock.*;
import com.greensock.easing.*;
var blitMask1:BlitMask = new BlitMask( strip1, strip1.x, strip1.y,
strip1.width, 100, true, true, 0xffff00, true);
spin_btn.addEventListener(MouseEvent.CLICK, spin);
function spin(event:MouseEvent):void {
spin_btn.visible = false;
var newNumber:Number = (randomNumber(0, 9) * 100) + 1200;
TweenMax.to(strip1, 1, {y:String(newNumber), onComplete:showBtn});
}
function showBtn() {
spin_btn.visible = true;
}
function randomNumber(min:Number, max:Number):Number {
return Math.floor(Math.random() * (1 + max - min) + min);
}
There is an object on stage called strip1 that have 10 children.Is there any way to find out which number(child) is on the stage after the virtual slot machine has stopped?
In other words, there is a tween method for an object and I want to find out whats its location after tween.
I recently developed a slot machine for the sake of it. I'm sure there are quite a few ways to do this but this is the way I did it. This has to be done for each reel.
There are usually three symbols visible inside your blitmask. If your blitmask is one symbol high then this method will give you the visible symbol
First declare a variable to keep track of the centre symbol.
I positioned my strip so that the 3rd symbol was center in the blitmask window, remember this is not the index.
Basically you know where it is going to land so you can find out what the symbol is before it lands.
var REEL1:Number = 3;
function spin(event:MouseEvent):void
{
var newNumber:Number = (randomNumber(1, 10)) ;
//call function to find the symbol to land
REEL1 = GetReelPosition(REEL1, newNumber);
//work out the amount to tween. Random number * symbol height + strip length
//if you want a longer spin add more strip lengths
newNumber = (newNumber * 100) + 1000;
var myTimeline:TimelineMax = new TimelineMax();
myTimeline.append(new TweenLite(Strip1, 2, {y:String(newNumber)}));
//get target symbol as movieclip
var currentTile:MovieClip = Strip1.getChildAt(REEL1-1) as MovieClip;
//if you have export for actionscript, this will tell what the object is
trace(String(currentTile));
}
When creating your strip, make symbols from your images, export for actionscript then add to your strip from top to bottom. If you change a symbol then you will have to delete and paste from top to bottom again. The index reflects the order the symbols are added.
function GetReelPosition(reel:Number,num:Number):Number
{
reel = reel - num;
switch(reel)
{
//if symbol is the first number on the strip and random
//number = 1 (1-1) then the centre symbol is the last on the strip
case 0:
reel = 10;
break;
//if symbol is the first number on the strip and random
//number = 10 (1-10) then the centre symbol is the first on the strip
case 1:
case -9:
reel = 1;
break;
case 2:
case -8:
reel = 2;
break;
case 3:
case -7:
reel = 3;
break;
case 4:
case -6:
reel = 4;
break;
case 5:
case -5:
reel = 5;
break;
case 6:
case -4:
reel = 6;
break;
case 7:
case -3:
reel = 7;
break;
case 8:
case -2:
reel = 8;
break;
//case 9, -1:
case 9:
case -1:
reel = 9;
break;
}
return reel;
}
I updated the switch code. case 9, -1: was not reliable. I'm not sure what language I picked that one from.
This was my first flash project and I did this slot machine fairly quickly. I'm sure there are better ways to do this. Have a look at my slot?
http://rcras.com/pokies/funnyfarm
Use a class variable to keep track of the currently visible element.
You can use the onCompleteParams argument of the TweenMax to pass the desired number. This would be available in the onComplete Handler. Set the variable when the tween complete (slot machine stops).
///...
var selectedChildIndex:Number = -1;
function spin(event:MouseEvent):void {
spin_btn.visible = false;
var newNumber:Number = (randomNumber(0, 9) * 100) + 1200;
TweenMax.to(strip1, 1, {y:String(newNumber),
onComplete:showBtn, onCompleteParams:[1]});
}
function showBtn(index) {
spin_btn.visible = true;
selectedChildIndex = index;
}