简体   繁体   English

通过标题名称访问 Google 电子表格数据

[英]Accessing Google Spreadsheet data via header names

Google Spreadsheet API let's you add rows to spreadsheets using the header names, this process is described here Google 电子表格 API 可让您使用标题名称向电子表格添加行, 此处描述此过程

The documentation however just gives an example of a simple case where the header text is made of of lowercase text (or text fitting the expression [a-z0-9]).然而,该文档仅给出了一个简单案例的示例,其中标题文本由小写文本(或符合表达式 [a-z0-9] 的文本)组成。

My application requires me to be able to set arbitrary header names for text (ie those that do not necessarily fit the expression above).我的应用程序要求我能够为文本设置任意标题名称(即那些不一定适合上述表达式的名称)。 Through much experimentation, I've figured out that special characters and symbols (apart from period) are generally not supported.通过大量实验,我发现通常不支持特殊字符和符号(句点除外)。 To access a spreadsheet that uses header rows containing special characters, the header text with the special characters removed has to be used.要访问使用包含特殊字符的标题行的电子表格,必须使用删除了特殊字符的标题文本。

These transformations are not documented and I have found them mostly by trial and error.这些转换没有记录在案,我主要是通过反复试验找到它们的。

For instance, to access a column with the header 'Foo Bar' via the API, the transformation of the header text to 'foobar' is required.例如,要通过 API 访问标题为'Foo Bar'的列,需要将标题文本转换为'foobar' Similarly, 'Foo.Bar' becomes 'foo.bar' .同样, 'Foo.Bar'变成'foo.bar'

Some special characters too have to be eliminated, however I keep getting corner cases in my code.一些特殊字符也必须删除,但是我的代码中不断出现极端情况。

What transformations need to be made to the actual header text to access it via the API?需要对实际标题文本进行哪些转换才能通过 API 访问它?

Also for this spreadsheet the transformation of the header text 'País' to 'país' doesn't seem to work.同样对于这个电子表格,标题文本'País''país'似乎不起作用。 I suspect it has something to do with the non-ASCII character 'í'我怀疑它与非 ASCII 字符'í'

Any suggestions will be greatly appreciated.任何建议将不胜感激。

You've got to remember when using 'List feed', it uses XML elements to delimit each value, taking the name of the field from Row 1 in the sheet.您必须记住,在使用“列表提要”时,它使用 XML 元素来分隔每个值,并从工作表中的第 1 行获取字段的名称。 Thus, the limits on the name are those on XML element names.因此,名称的限制是 XML 元素名称的限制。 There is an article on XML.com about it here .有关于它的XML.com文章在这里

As to the exact algorithm they use to transform various header-cell values into valid XML element names, I've never seen Google specify this (even though people have asked for clarification before now).至于他们用于将各种标题单元格值转换为有效 XML 元素名称的确切算法,我从未见过 Google 指定这一点(即使人们之前已经要求澄清)。 However, one might imagine it is something like:然而,人们可能会想象它是这样的:

foreach char in cell.value {
   if (isAllowed(char)) {
      name += char;
   }
}
return name;

Certainly, they do tend to omit spaces (for example) instead of converting them (as would be quite reasonable) to an underscore.当然,他们确实倾向于省略空格(例如)而不是将它们(这将是非常合理的)转换为下划线。

Now about characters outside of the ASCII set ...现在关于 ASCII 集之外的字符......

If your í is "LATIN SMALL LETTER I WITH ACUTE", then it's Unicode code-point is U+00ED, and it's written in XML-land as &#xED.如果您的 í 是“LATIN SMALL LETTER I WITH ACUTE”,那么它的 Unicode 代码点是 U+00ED,并且它在 XML-land 中编写为 &#xED。 As such, it's a valid character for an XML name.因此,它是 XML 名称的有效字符。 From http://www.w3.org/TR/REC-xml/#NT-NameChar , it does seem to be a valid NameStartCharhttp://www.w3.org/TR/REC-xml/#NT-NameChar似乎是一个有效的NameStartChar

 [4]    NameStartChar      ::=      ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
 [4a]       NameChar       ::=      NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
 [5]    Name       ::=      NameStartChar (NameChar)*

If they do have an algorithm like the above, then &#xED ought to be 'in'.如果他们确实有上述算法,那么 &#xED 应该是“in”。 However, I'd be paying close attention to encodings that you're sending the Spreadsheet API, and which it is sending back to you.但是,我会密切关注您发送电子表格 API 以及它发送回给您的编码。 I wouldn't put it past Google to have a bug in that respect.我不会让谷歌在这方面有错误。

You could of course do an experiment: put the values in the header row of a sheet using the Google Apps user-interface, and then doing a GET of the List feed, to see how the XML actually turns out.您当然可以做一个实验:使用 Google Apps 用户界面将值放在工作表的标题行中,然后对列表提要执行 GET,以查看 XML 的实际结果。 (But I expect you have been using this in your experiments so far). (但我希望您到目前为止一直在实验中使用它)。

Good luck.祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM