This repository was archived by the owner on Nov 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.gs
More file actions
163 lines (137 loc) · 6.71 KB
/
Copy pathCode.gs
File metadata and controls
163 lines (137 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//https://medium.com/@aio.phnompenh/make-ocr-tool-in-google-spreadsheet-to-extract-text-from-image-or-pdf-using-google-app-script-c478d4062b8c
//*******************************************************************************************************************//*******************************************************************************************************************
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('OCR Tools')
.addItem('Extract A Single Highlighted Cell', 'doOCR')
.addItem('Extract Multiple Highlighted Cells', 'doOCRALL')
.addToUi();
}
//*******************************************************************************************************************
//*******************************************************************************************************************
//Perform OCR on all items in the column
function AutodoOCR() {
//Force focus on 'OCR (Computers)' sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var computers = ss.getSheetByName('OCR (Computers)');
removeEmptyRows();
var lastRow = computers.getLastRow();
Logger.log("lastRow:" + lastRow);
//This is the column with the URL to the image
var activeCol = 5;
//var selected = computers.getRange(1, activeCol, lastRow, 1).getValues().length;
//Logger.log('selected length is '+selected);
var today = new Date();
Logger.log('Date is '+today);
var archiveFolder = DriveApp.getFolderById('15-B08m_5EUpVdqk65VuiAJq1g4bu09Lk');
//var selected = SpreadsheetApp.getActiveSheet().getActiveRange().getValues().length;
//for (var i = 0; i < selected; i++) {
//var activeRow = 1 + i;
if (computers.getRange(lastRow, activeCol + 1).isBlank()){
Logger.log('AutoOCR performed, Cell Content is '+computers.getRange(lastRow, activeCol + 1).getDisplayValue());
var valueURL = computers.getRange(lastRow , activeCol).getValue();
Logger.log('valueURL is '+valueURL);
if (valueURL != '#REF!'){
try{
var image = UrlFetchApp.fetch(valueURL).getBlob();
var file = {
title: 'OCR File '+today,
mimeType: 'image/png'
};
// OCR is supported for PDF and image formats
file = Drive.Files.insert(file, image, {ocr: true});
var doc = DocumentApp.openByUrl(file.embedLink);
var body = doc.getBody().getText();
//Get link Doc that Generated
computers.getRange(lastRow, activeCol + 2).setValue(file.embedLink);
//Get Content of Doc that Generated
computers.getRange(lastRow, activeCol + 1).setValue(body);
//Move OCR file from root Google Drive to picture folder
Logger.log('file.id is ' + file.id);
var fileByID = DriveApp.getFileById(file.id);
archiveFolder.addFile(fileByID);
DriveApp.getRootFolder().removeFile(fileByID);
//Backup picture file to folder
/* Logger.log(archiveFolder.getName());
var add = archiveFolder.createFile(image);
var attName = 'OCR File '+today;
add.setName(attName);
Logger.log(archiveFolder.getFiles());*/
} catch(e) {
Logger.log('Invalid URL');
}
//throw("testing complete");
//MailApp.sendEmail({to:'rmccal14+logger@uncc.edu',subject: "OCR Log!",body: Logger.getLog()});
}
} else {
Logger.log('Cell Content is '+computers.getRange(lastRow, activeCol + 1).getDisplayValue());
Logger.log('AutoOCR not performed');
}
//}
}
//*******************************************************************************************************************
function doOCR(image) {
//
var activeCol = SpreadsheetApp.getActiveSheet().getActiveCell().getColumn();
var activeRow = SpreadsheetApp.getActiveSheet().getActiveCell().getRow();
var activeCol2 = SpreadsheetApp.getActiveSheet().getDataRange().getLastColumn()
var activeRow2 = SpreadsheetApp.getActiveSheet().getDataRange().getLastColumn()
var valueURL = SpreadsheetApp.getActiveSheet().getRange(activeRow, activeCol).getValue();
var image = UrlFetchApp.fetch(valueURL).getBlob();
var file = {
title: 'OCR File',
mimeType: 'image/png'
};
// OCR is supported for PDF and image formats
file = Drive.Files.insert(file, image, {ocr: true});
var doc = DocumentApp.openByUrl(file.embedLink);
var body = doc.getBody().getText();
// Print the Google Document URL in the console
Logger.log("body: %s", body);
Logger.log("File URL: %s", file.embedLink);
//Get link Doc that Generated
SpreadsheetApp.getActiveSheet().getRange(activeRow, activeCol + 2).setValue(file.embedLink);
//Get Content of Doc that Generated
SpreadsheetApp.getActiveSheet().getRange(activeRow, activeCol + 1).setValue(body);
}
//*******************************************************************************************************************
function doOCRALL() {
var selected = SpreadsheetApp.getActiveSheet().getActiveRange().getValues().length;
for (var i = 0; i < selected; i++) {
var activeCol = SpreadsheetApp.getActiveSheet().getActiveCell().getColumn();
var activeRow = SpreadsheetApp.getActiveSheet().getActiveCell().getRow();
var valueURL = SpreadsheetApp.getActiveSheet().getRange(activeRow + i, activeCol).getValue();
var image = UrlFetchApp.fetch(valueURL).getBlob();
var file = {
title: 'OCR File',
mimeType: 'image/png'
};
// OCR is supported for PDF and image formats
file = Drive.Files.insert(file, image, {ocr: true});
var doc = DocumentApp.openByUrl(file.embedLink);
var body = doc.getBody().getText();
//Get link Doc that Generated
SpreadsheetApp.getActiveSheet().getRange(activeRow + i, activeCol + 2).setValue(file.embedLink);
//Get Content of Doc that Generated
SpreadsheetApp.getActiveSheet().getRange(activeRow + i, activeCol + 1).setValue(body);
}
}
//*******************************************************************************************************************
//https://stackoverflow.com/questions/44579300/how-to-ignore-empty-cell-values-for-getrange-getvalues
//Delete empty rows
function removeEmptyRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet=ss.getSheetByName("OCR (Computers)");
var maxRows = sheet.getMaxRows();
var result = sheet.getRange("A1:A").getValues();
var lastRow = [i for each (i in result) if (isNaN(i))].length;
Logger.log("sheet is "+sheet.getName());
Logger.log("lastRow is "+lastRow);
if (maxRows-lastRow > 1){
Logger.log("Delete Rows");
sheet.deleteRows(lastRow+1, maxRows-lastRow);
} else {
Logger.log("Don't Delete Rows");
}
}
//*******************************************************************************************************************