i have the following problem. i import an yaml file and parse it into a string. now i want to seperate the string and fill an array with the string components.
en_label: german and english
this is the string and i want to add "german and english" into an array
I'd grateful for any suggestions
loadLang(){
const yaml = require('js-yaml');
// Get document, or throw exception on error
const doc = yaml.load('./data/lang.yaml', 'utf8');
//console.log(doc);
//todo parse string
const langString = String(doc)
console.log(langString)
return langString
}
beforeCreate(){
const lang = this.loadLang()
for(let i = 0; i<lang.length;i++){
this.lang_list.push(e)
}
}
Related
Universal Binary JSON
I'm on a vue.js v2 project. I need to parse a token after logged in. It is not a JWT token. It's a Ubjson token. I am using javascript, and I can't find any Ubjson token decoder anywhere.
If you know how to decode it, please help me out.
I've tried
https://www.npmjs.com/package/#shelacek/ubjson
import { Ubjson } from '#shelacek/ubjson'
const obj = Ubjson.decode(token)
console.log(obj) //undefined
Result
undefined
These solutions also work for me with fewer lines of code.
var ubjson = require("#shelacek/ubjson")
const token = '123***XYZ'
const buffer = Buffer.from(token, 'base64')
const obj = ubjson.decode(buffer)
console.log(obj)
There is a UBJSON encoder/decoder on NPM
See code-8's answer
Try something like this:
See in action [https://runkit.com/embed/3ngoupnl0unj]=
const ubjson = require('#shelacek/ubjson');
// https://stackoverflow.com/a/62364519/3330981
function base64ToBytesArr(str) {
const abc = [..."ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"]; // base64 alphabet
let result = [];
for(let i=0; i<str.length/4; i++) {
let chunk = [...str.slice(4*i,4*i+4)]
let bin = chunk.map(x=> abc.indexOf(x).toString(2).padStart(6,0)).join('');
let bytes = bin.match(/.{1,8}/g).map(x=> +('0b'+x));
result.push(...bytes.slice(0,3 - (str[4*i+2]=="=") - (str[4*i+3]=="=")));
}
return result;
}
const token = "e2kEdmVyc0kAAWkEdXNlcntpBG5hbWVTaRNiaGVuZ0B2aTNnbG9iYWwuY29taQRhcHBsU2kCVkNpBHNlcnZbJFUjaRC8L+g8i58TZQsAWbfrIuGgaQRwZXJtW3tpBG5hbWVTaQNFSURpBHBhcm1be2kEbmFtZVNpA0FQUGkEZGF0YVNpASp9XWkEZGF0YVNpSkJST08sQ05WRixDTlZaLERGUUwsREoxTCxGVjhaLEhZMEIsTjk0WCxSRDhMLFczWFYsWDNDWSxYUEg0LFlYNE4sWlIxMCxDT0xNfXtpBG5hbWVTaQNFSURpBHBhcm1be2kEbmFtZVNpA0FQUGkEZGF0YVNpAlZDfV1pBGRhdGFTaQlDT0xNLERGUUx9kEbmFtZVNpClZDX0dFVF9FSURpBHBhcm1be2kEbmFtZVNpBUJSQU5EaQRkYXRhU2kJQlJPTyxDT0xNfV1pBGRhdGFUfV19aQRyZWxtWyRVI2kQqBr68fUnmySYz31zLMgdOWkEYWxnb1NpClNIQTI1Ni1SU0FpBHNpZ25bJFUjSQCAokw8V8p06MkGqbm4Dry0ymUNQ2AM9TFd77vjFfJS3qHaGf/DYyYmkZamXUhOqFaucdVtkEgyQYMrDM2vOMILH7YnUVmgty8nH5Oim5w4aRQRepLcd14E98BnRjHovRS3KqeHoiON78gsTTcwhX1CE1hXlZxZGr6EJleEr8HbmMt9"
const obj = ubjson.decode(base64ToBytesArr(token));
console.log(obj);
I'm using the Filesaver.js library to export an .xlsx file from a table I own. But I am getting some errors. See below:
Original table created using VueJs with Quasar Framework:
Generated .xlsx file:
Another case
Generated .xlsx file:
My code:
exportTable() {
let wb = XLSX.utils.table_to_book(document.querySelector('.q-table'), {
sheet: "Sheet JS",
})
let wbout = XLSX.write(wb, {
bookType: 'xlsx',
bookSST: true,
type: 'binary'
})
function s2ab(s) {
let buf = new ArrayBuffer(s.length)
let view = new Uint8Array(buf)
for (let i = 0; i < s.length; i++) view[i] = s.charCodeAt(i) & 0xFF
return buf
}
saveAs(new Blob([s2ab(wbout)], {
type: "text/plain;charset=utf-8"
}), 'test.xlsx')
Why is the format of the characters in the table not being kept when exporting the spreadsheet ?
Is my spreadsheet data not treated as a string?
I'm brazilian. Sorry for bad English (=
After a few days of looking for answers, I managed to solve my problem.
More precisely, just use {raw: true}. By doing this, the lib no longer formats the data, leaving it in the raw form that comes from HTML. Interestingly, I didn't find this in the documentation.
// import something here
import Vue from 'vue'
import XLSX from 'xlsx'
import {
saveAs
} from 'file-saver'
// Global Function
const exportExcel = (table) => {
let wb = XLSX.utils.table_to_book(table, {
sheet: "Sheet JS",
raw: true // Here
})
let wbout = XLSX.write(wb, {
bookType: 'xlsx',
bookSST: true,
type: 'binary'
})
function s2ab(s) {
let buf = new ArrayBuffer(s.length)
let view = new Uint8Array(buf)
for (let i = 0; i != s.length; i++) view[i] = s.charCodeAt(i) & 0xFF
return buf
}
saveAs(new Blob([s2ab(wbout)], {
type: "text/plain;charset=utf-8"
}), 'spreadsheet.xlsx')
}
Vue.prototype.$exportExcel = exportExcel;
// "async" is optional;
// more info on params: https://quasar.dev/quasar-cli/cli-documentation/boot-files#Anatomy-of-a-boot-file
//export default exportExcel
This link helped me
I am very new to Google Script. I have some pdf files in a folder on Google Drive and I am trying to convert pdf to google doc and extract specific text. PDF has more than 200 pages but even the google.doc file is limited to 80 pages. Is there a limit on number of pages you can run OCR on? Or I am missing something....
My code below:
//#####GLOBALS#####
const FOLDER_ID = "1rlAL4WrnxQ6pEY2uOmzWA_csUIDdBjVK"; //Folder ID of all PDFs
const SS = "1XS_YUUdu9FK_bBumK3lFu9fU_M9w7NGydZqOzu9vTyE";//The spreadsheet ID
SHEET = "Extracted";//The sheet tab name
/*########################################################
Main run file: extracts student IDs from PDFs and their
section from the PDF name from multiple documents.
Displays a list of students and sections in a Google Sheet.
*/
function extractInfo(){
const ss = SpreadsheetApp.getActiveSpreadsheet()
//Get all PDF files:
const folder = DriveApp.getFolderById(FOLDER_ID);
//const files = folder.getFiles();
const files = folder.getFilesByType("application/pdf");
let allInfo = []
//Iterate through each folderr
while(files.hasNext()){
Logger.log('first call');
let file = files.next();
let fileID = file.getId();
const doc = getTextFromPDF(fileID);
const invDate = extractInvDate(doc.text);
allInfo = allInfo.concat(invDate);
Logger.log("Length of allInfo array: ")
Logger.log(allInfo.length);
}
importToSpreadsheet(allInfo); //this is 80, even though pdf
//has more than 200 pages with
//required text (invoice date) on each page
};
/*########################################################
* Extracts the text from a PDF and stores it in memory.
* Also extracts the file name.
*
* param {string} : fileID : file ID of the PDF that the text will be extracted from.
*
* returns {array} : Contains the file name and PDF text.
*
*/
function getTextFromPDF(fileID) {
var blob = DriveApp.getFileById(fileID).getBlob()
var resource = {
title: blob.getName(),
mimeType: blob.getContentType()
};
var options = {
ocr: true,
ocrLanguage: "en"
};
// Convert the pdf to a Google Doc with ocr.
var file = Drive.Files.insert(resource, blob, options);
// Get the texts from the newly created text.
var doc = DocumentApp.openById(file.id);
var text = doc.getBody().getText();
var title = doc.getName();
// Deleted the document once the text has been stored.
Drive.Files.remove(doc.getId());
return {
name:title,
text:text
};
}
function extractInvDate(text){
const regexp = /Invoice Date:/g;//commented out \d{2}\/\d{2}\/\d{4}/gi;
try{
let array = [...text.match (regexp)];
return array;
}catch(e){
}
};
function importToSpreadsheet(data){
const sheet = SpreadsheetApp.openById(SS).getSheetByName(SHEET);
const range = sheet.getRange(3,1,data.length,1);
var j = 0;
for (j = 0; j < data.length; j++){
Logger.log(j);
range.getCell(j+1,1).setValue(data[j]);
}
//range.sort([2,1]);
}
The problem or limitation is with the Drive.Files.insert function
When the blob is extracted, fetch the string but it has mime details also...one may need to process it. Sample code is below. modify as per ur need
var blob = DriveApp.getFileById(fileID).getBlob()
var txt = blob.getDataAsString()
I want to test a view that has a list and filtering functionality:
I want to check the text of the first row and save it
Filter using that text
Check again that the same element is rendered
Thing is, when I match and element using element(by.id('some-id')), how can I retrieve info from that element (if it is possible) like the text it contains?
I created the detox-getprops npm package incorporating the hack mentioned by Maxime Helen. It allows retrieving the text and some other (platform-dependent) properties of the element.
const { getText } = require('detox-getprops');
const text = await getText(element(by.id('heading')));
expect(text).toEqual('Step One');
I hope Detox #445 will be resolved soon after which this package can be deprecated.
Update: You can now fetch the text on iOS using the getAttributes method. The detox-getprops library is still needed for Android (tracked using Detox #2083).
Hacky/funny workaround elaborated from this comment:
const readVisibleText = async (testID) => {
try {
await expect(element(by.id(testID))).toHaveText('_unfoundable_text');
throw 'We never should get here unless target element has unfoundable text';
} catch (error) {
if (device.getPlatform() === 'ios') {
const start = `accessibilityLabel was "`;
const end = '" on ';
const errorMessage = error.message.toString();
const [, restMessage] = errorMessage.split(start);
const [label] = restMessage.split(end);
return label;
} else {
// Android to be tested
const start = 'Got:';
const end = '}"';
const errorMessage = error.message.toString();
const [, restMessage] = errorMessage.split(start);
const [label] = restMessage.split(end);
const value = label.split(',');
var combineText = value.find(i => i.includes('text=')).trim();
const [, elementText] = combineText.split('=');
return elementText;
}
}
};
This is currently not supported, the progress is being tracked in this issue.
I'm trying out Dart, but I cant figure out, how to send an image from the user to the server. I have my input-tag, and i can reach this in the DART code, but i cant seem to read from it. Im trying something like:
InputElement ie = document.query('#myinputelement');
ie.on.change.add((event){<br/>
InputElement iee = document.query('#myinputelement');<br/>
FileList mfl = iee.files;<br/>
File myFile = mlf.item(0);<br/>
FileReader fr = new FileReader();
fr.readAsBinaryString(myFile);
String result = fr.result; //this is always empty
});
With the html containing:
<input type="file" id="myinputelement">
I really hope you cant help me, im kinda stuck. I might just be missing how to do the onload for the filereader, or maybe im doing it totally wrong.
The FileReader API is asynchronous so you need to use event handlers.
var input = window.document.querySelector('#upload');
Element log = query("#log");
input.addEventListener("change", (e) {
FileList files = input.files;
Expect.isTrue(files.length > 0);
File file = files.item(0);
FileReader reader = new FileReader();
reader.onLoad = (fileEvent) {
print("file read");
log.innerHTML = "file content is ${reader.result}";
};
reader.onerror = (evt) => print("error ${reader.error.code}");
reader.readAsText(file);
});
you also need to allow file uploads from to your browser, which can be done in Chrome by starting it with the flag --allow-file-access-from-files
This is how to read a file using dart:html.
document.querySelector('#myinputelement`).onChange.listen((changeEvent) {
List fileInput = document.querySelector('#myinputelement').files;
if (fileInput.length > 1) {
// More than one file got selected somehow, could be a browser bug.
// Unless the "multiple" attribute is set on the input element, of course
}
else if (fileInput.isEmpty) {
// This could happen if the browser allows emptying an upload field
}
FileReader reader = new FileReader();
reader.onLoad.listen((fileEvent) {
String fileContent = reader.result;
// Code doing stuff with fileContent goes here!
});
reader.onError.listen((itWentWrongEvent) {
// Handle the error
});
reader.readAsText(fileInput[0]);
});
It's not necessary (any more) to use dart:dom FileReader instead of the one from dart:html.
Your code should work if you add an event listener to the file reader, like this:
FileReader fr = new FileReader();
fr.on.load.add((fe) => doSomethingToString(fe.target.result));
fr.readAsBinaryString(myFile);
My attempt
void fileSelected(Event event) async {
final files = (event.target as FileUploadInputElement).files;
if (files.isNotEmpty) {
final reader = new FileReader();
// ignore: unawaited_futures
reader.onError.first.then((evt) => print('error ${reader.error.code}'));
final resultReceived = reader.onLoad.first;
reader.readAsArrayBuffer(files.first);
await resultReceived;
imageReference.fileSelected(reader.result as List<int>);
}
}
Thanks to the help from this post, I got it to work. I still utilized my event handler in the input tag and made sure that I DID NOT import both dart:io and dart:html, only dart:html is needed.
This is what my final AppComponent looked like.
import 'dart:html';
import 'package:angular/angular.dart';
#Component(
selector: 'my-app',
styleUrls: ['app_component.css'],
templateUrl: 'app_component.html',
directives: [coreDirectives],
)
class AppComponent {
// Stores contents of file upon load
String contents;
AppComponent();
void fileUpload(event) {
// Get tag and the file
InputElement input = window.document.getElementById("fileUpload");
File file = input.files[0];
// File reader and event handler for end of loading
FileReader reader = FileReader();
reader.readAsText(file);
reader.onLoad.listen((fileEvent) {
contents = reader.result;
});
}
}
This is what my template looks like:
<h1>File upload test</h1>
<input type="file" (change)="fileUpload($event)" id="fileUpload">
<div *ngIf="contents != null">
<p>Hi! These are the contents of your file:</p>
<p>{{contents}}</p>
</div>