node.jsgoogle-apps-scriptgoogle-forms-api

Generate itemId when batchUpdating with Forms API


Please what are the constraints in generating an itemId. I generate unique itemId for each item in the form, but the API keeps telling me invalid ID.

https://developers.google.com/forms/api/reference/rest/v1/forms#Item

Please I need help with this

{
    "includeFormInResponse": false,
    "requests":     [
        {
            "createItem": {
                "item": {
                    "itemId": "4e637fjc",
                    "description": "First Name",
                    "questionItem": {
                        "question": {
                            "textQuestion": {
                                "paragraph": false
                            },
                            "required": true
                        }
                    }
                },
                "location": {
                    "index": 0
                }
            }
        },
        {
            "createItem": {
                "item": {
                    "itemId": "njyf3izr",
                    "description": "Middle Name",
                    "questionItem": {
                        "question": {
                            "textQuestion": {
                                "paragraph": false
                            },
                            "required": true
                        }
                    }
                },
                "location": {
                    "index": 1
                }
            }
        },
    }
]

Solution

  • When I had tested Google Forms API before, unless I'm mistaken, I had thought that the rule of item ID might be required to be 00000000 to 7fffffff as the hex value. By the way, for example, 0 is used as 00000000.

    When I saw your showing request body, you are trying to use 4e637fjc and njyf3izr as the item ID. In the case of these values, the values are not hex values. I thought that by this, an error like Invalid ID occurred.

    But, I think that actually, this is not published in the official document. So, I would like to tell this.

    Added:

    About your following reply,

    Do you mean something like this, with Javascript. crypto.randomBytes(256).toString('hex').slice(0, 8)

    From your tag, when you want to use Google Apps Script or Node.js, how about the following sample script? Unfortunately, Google Apps Script cannot directly use "crypto". So, I proposed the following sample script.

    Sample script:

    const res = Math.floor(Math.random() * parseInt("7FFFFFFF", 16)).toString(16).padStart(8, "0");
    console.log(res);