Skip to content

Instantly share code, notes, and snippets.

@Asano-Naoki
Last active February 9, 2025 15:53
Show Gist options
  • Save Asano-Naoki/aa2af8f299715c1e31a3c87443d6e2dd to your computer and use it in GitHub Desktop.
Save Asano-Naoki/aa2af8f299715c1e31a3c87443d6e2dd to your computer and use it in GitHub Desktop.
document ai ocr
{
"timeZone": "Asia/Tokyo",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
function handle_json() {
var fileIT = DriveApp.getFileById('1CVMsiZ1FOPzM6K4AcDG24Qn0REwBE5_p');
var textdata = fileIT.getBlob().getDataAsString('utf8');
var jobj = JSON.parse(textdata);
var forms = jobj["pages"][0]["formFields"];
for (form in forms) {
Logger.log(forms[form]["fieldName"]["textAnchor"]["content"]);
}
return jobj;
}
function getAccessToken() {
const options = {
"method": "POST",
"payload": {
"grant_type": 'urn:ietf:params:oauth:grant-type:jwt-bearer',
"assertion": getAssertion()
},
'muteHttpExceptions': true,
};
const response = JSON.parse(UrlFetchApp.fetch('https://oauth2.googleapis.com/token', options));
return response.access_token;
}
function getAssertion() {
const properties = PropertiesService.getScriptProperties();
const privateKey = properties.getProperty('privateKey').replace(/\\n/g, '\n');
const header = {
alg: 'RS256',
typ: 'JWT'
};
const now = new Date();
const claimSet = {
iss: "[email protected]",
scope: "https://www.googleapis.com/auth/cloud-platform",
aud: "https://accounts.google.com/o/oauth2/token",
exp: (now.getTime() / 1000) + 3000,
iat: now.getTime() / 1000
};
let toSign = Utilities.base64EncodeWebSafe(JSON.stringify(header)) + '.' + Utilities.base64EncodeWebSafe(JSON.stringify(claimSet));
toSign = toSign.replace(/=+$/, '');
const signatureBytes = Utilities.computeRsaSha256Signature(toSign, privateKey);
let signature = Utilities.base64EncodeWebSafe(signatureBytes);
signature = signature.replace(/=+$/, '');
return toSign + '.' + signature;
};
function getGcpPolicies() {
const options = {
"method": "POST",
"contentType": "application/json",
"headers": {
"Authorization": "Bearer " + getAccessToken(),
},
'muteHttpExceptions': true,
};
const response = JSON.parse(UrlFetchApp.fetch('https://cloudresourcemanager.googleapis.com/v1/projects/gspread-doc-test:getIamPolicy', options).getContentText());
const policies = response.bindings;
console.log(policies)
}
function getFileInformations () {
//var apiEndpoint = "https://eu-documentai.googleapis.com/v1beta3/projects/"+PROJECT_ID+"/locations/eu/processors/" + PROCESSOR_ID:method
// var apiEndpoint = "https://eu-documentai.googleapis.com/v1beta3/projects/"+PROJECT_ID+"/locations/eu/processors/" + PROCESSOR_ID + ":process";
const apiEndpoint = 'https://us-documentai.googleapis.com/v1/projects/908848982543/locations/us/processors/db028362e8018c92/processorVersions/pretrained-form-parser-v2.0-2022-11-10:process';
var token = ScriptApp.getOAuthToken();
// encode
const blob = DriveApp.getFileById('1InRAiOmOKOZvu6MqMdOBk8N6HF0Ay67o').getBlob()
const encoded = Utilities.base64Encode(blob.getBytes());
// Create our json request
var param = {
"rawDocument": {
"mimeType":'application/pdf',
"content": encoded
}
};
// console.log(param);
console.log(getAccessToken());
var jsonresquest=JSON.stringify(param);
var options = {};
options.headers = {'Authorization':"Bearer "+getAccessToken(), 'Content-Type': "application/json; charset=utf-8"};
// options.headers = {'Authorization':"Bearer "+ token, 'Content-Type': "application/json; charset=utf-8"};
options.payload = JSON.stringify(param);
//options.payload = param;
options.method = "POST";
options.muteHttpExceptions=true;
// Logger.log("options.payload : " + options.payload);
//console.log(options);
Logger.log(DriveApp.getFileById('1InRAiOmOKOZvu6MqMdOBk8N6HF0Ay67o').getBlob());
// And make the call
var response = UrlFetchApp.fetch(apiEndpoint, options);
var result = JSON.parse(response);
//console.log(response)
Logger.log(result["document"]["pages"][0]["formFields"]);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment