App inventor 2 en español
Cómo programar los teléfonos móviles con Android.
Curso de App inventor 2 en español - Juan Antonio Villalpando
-- Tutorial de iniciación de App Inventor 2 en español --
Volver al índice del tutorial
___________________________
70A.- Excel a Json. Archivo a Base64. Leer archivo. JavaScript.
p70_base64_javascript.aia
p70B_excel_javascript.aia
p70C_lee_archivo_javascipt.aia
- Estos códigos los he puesto en la Comunidad de App Inventor.
Excel xlsx file to Json with JavaScript
File to Base64 with JavaScript
File to ASCII with JavaScript. Read text file with JavaScript
________________________________________________________________________________________
ToBase64.htm |
<script>
async function readTextFileAsBlob(file) {
const response = await fetch(file);
const blob = await response.blob();
return blob;
}
const blobToBase64DataURL = blob => new Promise(
resolvePromise => {
const reader = new FileReader();
reader.onload = () => resolvePromise(reader.result);
reader.readAsDataURL(blob);
}
);
my_file = AppInventor.getWebViewString(); // INPUT
readTextFileAsBlob(my_file).then(
async blob => {
const base64URL = await blobToBase64DataURL(blob);
// document.write(base64URL);
AppInventor.setWebViewString(base64URL); // OUTPUT
}
);
</script>
|
excel_to_json.htm |
<script src="xlsx.full.min.js"></script>
<script>
// Adaptado por kio4.com de: https://blog.bitsrc.io/csv-excel-to-json-in-javascript-70e61a1dc32d
my_file = AppInventor.getWebViewString(); // INPUT
// read Excel file and convert to json format using fetch
fetch(my_file).then(function (res) {
/* get the data as a Blob */
if (!res.ok) throw new Error("fetch failed");
return res.arrayBuffer();
})
.then(function (ab) {
/* parse the data when it is received */
var data = new Uint8Array(ab);
var workbook = XLSX.read(data, {
type: "array"
});
/* *****************************************************************
* DO SOMETHING WITH workbook: Converting Excel value to Json *
********************************************************************/
var first_sheet_name = workbook.SheetNames[0];
/* Get worksheet */
var worksheet = workbook.Sheets[first_sheet_name];
var _JsonData = XLSX.utils.sheet_to_json(worksheet, {raw: true});
/************************ End of conversion ************************/
// AppInventor.setWebViewString(JSON.stringify(_JsonData)); // No Pretty
AppInventor.setWebViewString(JSON.stringify(_JsonData, undefined, 2)); // OUTPUT
});
</script>
|
read_file.htm |
<script>
my_file = AppInventor.getWebViewString(); // INPUT
fetch(my_file).then(function (res) {
/* get the data as a Blob */
if (!res.ok) throw new Error("fetch failed");
return res.arrayBuffer();
})
.then(function (ab) {
var data = new Uint8Array(ab);
// AppInventor.setWebViewString("" + data); // OUTPUT - GET ASCII
var str = new TextDecoder().decode(Uint8Array.from(data));
AppInventor.setWebViewString("" + str); // OUTPUT
});
</script>
|
__________________________________
|