Generate itemId when batchUpdating with Forms API

Viewed 78

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
                }
            }
        },
    }
]
2 Answers

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);

  • In this sample script, the values of 00000000 to 7fffffff are randomly returned.

Missing documentation

I am afraid that since the Forms API is very new there is no documentation about the specific format the ID should have.

I have done a couple of tests with the API and the only thing I was able to figure out is that the ID needs an 8-character-long string to work, otherwise it would not work or would fill out the blank spaces with zeros.

When doing the testing I was also able to find out that sometimes the API would take a specific pattern of letters and numbers, but when changing the numbers and letters it stops working for no reason.

This seems like missing clarification from the documentation, and I would strongly recommend sending feedback about this problem on the API method page. You can do so by clicking the following option at the top right corner of the documentation:

enter image description here

Google tends to check that feedback a lot when talking about missing information. In addition to all that you can also fill out a report in Google's issue tracker so that they investigate the inconsistencies when using the batchUpdate method to update the ID.

References:

Related