easymacro/docs/es/docs/examples/index.md

155 lines
3.8 KiB
Markdown
Raw Normal View History

2023-04-24 23:09:03 -06:00
---
title: Índice
---
# Ejemplos reales de uso
---
## Calc
---
### Solicitar precios
Consultar datos en json a una página web y vaciarlos en una hoja.
=== "EasyMacro"
```py
import easymacro as app
def obtener_precios():
URL = 'https://api.binance.com/api/v3/ticker/price'
respuesta = app.url.get(URL)
if respuesta.status_code == 200:
datos = respuesta.json()
app.active.active['A1'].dict = datos
return
```
=== "Sin EasyMacro"
```py
import uno
import json
from urllib import request
def obtener_precios():
URL = 'https://api.binance.com/api/v3/ticker/price'
data = json.loads(request.urlopen(URL).read().decode())
doc = XSCRIPTCONTEXT.getDocument()
sheet = doc.CurrentController.ActiveSheet
sheet[0,0].String = 'symbol'
sheet[0,1].String = 'price'
for i, row in enumerate(data):
sheet[i + 1,0].String = row['symbol']
sheet[i + 1,1].Value = float(row['price'])
return
```
<br>
### Clonar imagenes
Clonar todas las imagenes de una hoja a otra.
=== "EasyMacro"
```py
doc = app.active
for image in doc[0].dp:
if image.is_image:
image.clone(doc[1].dp)
```
=== "Sin EasyMacro"
```py
import uno
import io
import unohelper
from com.sun.star.io import IOException, XOutputStream
from com.sun.star.beans import PropertyValue
CTX = uno.getComponentContext()
SM = CTX.getServiceManager()
class LOShape():
def __init__(self, shape):
self._shape = shape
class OutputStream(unohelper.Base, XOutputStream):
def __init__(self):
self._buffer = b''
self.closed = 0
@property
def buffer(self):
return self._buffer
def closeOutput(self):
self.closed = 1
def writeBytes(self, seq):
if seq.value:
self._buffer = seq.value
def flush(self):
pass
def clone_image(self, doc, to_sheet):
stream = self._shape.GraphicStream
buffer = self.OutputStream()
size, data = stream.readBytes(buffer, stream.available())
stream = SM.createInstanceWithContext('com.sun.star.io.SequenceInputStream', CTX)
stream.initialize((data,))
image = doc.createInstance('com.sun.star.drawing.GraphicObjectShape')
gp = SM.createInstance('com.sun.star.graphic.GraphicProvider')
properties = (PropertyValue(Name='InputStream', Value=stream),)
image.Graphic = gp.queryGraphic(properties)
to_sheet.DrawPage.add(image)
image.Size = self._shape.Size
image.Position = self._shape.Position
return
def clone_shape(self, doc, to_sheet):
for p in self._shape.CustomShapeGeometry:
if p.Name == 'Type':
type_shape = p.Value.title()
service = f'com.sun.star.drawing.{type_shape}Shape'
shape = doc.createInstance(service)
shape.Size = self._shape.Size
shape.Position = self._shape.Position
to_sheet.DrawPage.add(shape)
return
def main():
IMAGE = 'com.sun.star.drawing.GraphicObjectShape'
SHAPE = 'com.sun.star.drawing.CustomShape'
doc = XSCRIPTCONTEXT.getDocument()
source = doc.CurrentController.ActiveSheet
target = doc.Sheets[1]
for shape in source.DrawPage:
s = LOShape(shape)
if shape.ShapeType == IMAGE:
s.clone_image(doc, target)
elif shape.ShapeType == SHAPE:
s.clone_shape(doc, tar
```
<br>