2

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
                }
            }
        },
    }
]
Nathaniel Babalola
  • 617
  • 2
  • 6
  • 15

2 Answers2

3

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.
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • ooh, thank you. Do you mean something like this, with Javascript. ```crypto.randomBytes(256).toString('hex').slice(0, 8)``` @tanaike – Nathaniel Babalola Sep 01 '22 at 06:42
  • @Nathaniel Babalola Thank you for replying. About your reply, I updated my answer. Could you please confirm it? If that was not useful, I apologize. – Tanaike Sep 01 '22 at 07:07
  • I just tried it and it worked, thank you so much. I don't really understand the code though, If It's not too much to ask could you please add some more explanation of what is happening and why. Thank you so much. @tanaike – Nathaniel Babalola Sep 01 '22 at 07:19
  • @Nathaniel Babalola Thank you for replying. About `I just tried it and it worked, thank you so much.`, I'm glad your issue was resolved. About `I don't really understand the code though, If It's not too much to ask could you please add some more explanation of what is happening and why. Thank you so much`, my suggested script returns the values of `00000000` to `7fffffff` as the hex. The flow is as follows. – Tanaike Sep 01 '22 at 07:26
  • 2
    @Nathaniel Babalola `Math.floor(Math.random() * parseInt("7FFFFFFF", 16))` returns the decimal of the values of `00000000` to `7fffffff` as the integer. And the decimal is converted to the hex by `toString(16)`. And, when the length of the result value is less than 8, `0` is added by `padStart(8, "0")`. `7FFFFFFF` is `2147483647` as the decimal number. So, `parseInt("7FFFFFFF", 16)` can be replaced with `2147483647`. But, I thought that when `7FFFFFFF` is used, it might help understand the script. So, I used it. If this explanation was not useful, I apologize. – Tanaike Sep 01 '22 at 07:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/247731/discussion-between-nathaniel-babalola-and-tanaike). – Nathaniel Babalola Sep 01 '22 at 11:22
0

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:

Fernando Lara
  • 2,263
  • 2
  • 4
  • 14