Fill forms in a document on Nextcloud. The FormField type has to be specified.
One script reply has been approved by the moderators Verified

Fills Rich Text forms in a richdocuments document

Created by nextcloud 400 days ago Picked 5 times
Submitted by nextcloud Bun
Verified 21 days ago
1
import axios from "axios"
2

3
export async function main(
4
  nextcloud: RT.Nextcloud,
5
  templateFileId: number,
6
  formFieldValue: string, 
7
  destination: string, 
8
  fields: { [index: string]: string },
9
  convertToPdf: boolean = false,
10
) {
11

12
  let data: any = { fields: {}, destination };
13

14
  const selectors: { [index: string]: string } = {
15
    id: 'ById',
16
    title: 'ByAlias',
17
    index: 'ByIndex',
18
    tag: 'ByTag',
19
  }
20
  const selector: string = selectors[formFieldValue] ?? 'ById'
21
  console.debug('SELECTOR ----', selector)
22
  data.fields = Object.keys(fields).reduce((carry: { [index: string]: object }, key: string) => {
23
    carry['ContentControls.' + selector + '.' + key] = { content: fields[key] }
24
    return carry
25
  }, {});
26
  
27
  if (convertToPdf) {
28
    data.convert = 'pdf'
29
  }
30
  
31
  console.debug('data', data)
32

33
  const url = nextcloud.baseUrl + '/ocs/v2.php/apps/richdocuments/api/v1/template/fields/fill/' + templateFileId
34
  const config = {
35
    auth: {
36
      username: nextcloud.userId,
37
      password: nextcloud.token,
38
    },
39
    headers: {
40
      'content-type': 'application/json',
41
      'ocs-apirequest': true,
42
    },
43
  }
44
  console.debug('config', config)
45
  try {
46
    const resp = await axios.post(url, data, config)
47
    console.debug('RESPONSE', resp.data)
48
    return {
49
      data: resp.data,
50
    }
51
  } catch(e) {
52
    console.debug('error', e)
53
  }
54
  
55
  
56
  return {}
57
}