-
Notifications
You must be signed in to change notification settings - Fork 0
/
importToNewColumns.js
60 lines (53 loc) · 1.69 KB
/
importToNewColumns.js
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
// ========== importToNewColumns Start ========== //
// NAME: importToNewColumns
// VERSION: 1.0.3
/**
* Imports a file and adds the right columns
*
* filename: The file to import.
* indexKey: The field ID of the key field in the table field.
*
* return: Empty return value in all cases.
*/
document.getFormNamed('Script Manager').runScriptNamed('Logger');
function importToNewColumns(form, uri, indexKey) {
console.log("Index Key: " + indexKey);
const indexKeyId = form.getFieldNamed(indexKey).getId();
console.log("Index Key Id: " + indexKeyId);
const jsonFile = uri;
let rowList = Utils.getJsonFromUrl(jsonFile);
if (!rowList) {
console.log("No index file?");
return
}
for (row of rowList) {
console.log("Row: " + JSON.stringify(row))
let rowId = row[indexKey];
console.log("Row Id: " + rowId);
console.log("Name: " + form.name);
let targetRecord = getRecordFromFormWithKey(form.name,indexKeyId, rowId);
if (!targetRecord) {
console.log("Adding new record...");
targetRecord = form.addNewRecord();
}
console.log("Record: " + targetRecord.getId());
for (const [key, value] of Object.entries(row)) {
let keyField = form.getFieldNamed(key);
if (!keyField) {
console.log("Adding field: " + key + "Type: " + typeof(value));
if (typeof(value) == "number") {
fieldType = "number";
}
else {
fieldType = "text"
}
keyField = form.addNewFieldNamedWithType(key,fieldType);
form.saveAllChanges();
}
keyId = keyField.getId();
console.log("Key: " + key + " keyId: " + keyId + " Value: " + value);
targetRecord.setFieldValue(keyId, value);
}
}
form.saveAllChanges();
}