从网络检测区域设置(列表分隔符)

发布于 2024-07-24 16:43:37 字数 366 浏览 10 评论 0 原文

在对逗号分隔值 (CSV) 文件不一定是逗号分隔感到不愉快之后,我试图找出是否有任何方法可以通过 http 请求检测客户端计算机上的区域设置列表分隔符值。

场景如下:用户可以从网站下载一些 CSV 格式的数据(RoR,如果重要的话)。 该 CSV 文件是动态生成的,发送给用户,大多数情况下双击并在目标 Windows 计算机上的 MS Excel 中打开。 现在,如果用户将“,”设置为列表分隔符,则数据会正确排列在列中,但如果设置了任何其他分隔符(此处广泛使用“;”),则所有数据都会被放入单个列中。 那么,有什么方法可以检测客户端计算机上使用的分隔符,并相应地生成文件吗?

我有一种沉闷的感觉,事实并非如此,但我想在向客户传递“无法完成,抱歉”之前确定一下:)

After having the unpleasant surprise that Comma Seperated Value (CSV) files are not necessarily comma-separated, I'm trying to find out if there is any way to detect what the regional settings list separator value is on the client machine from http request.

Scenario is as follows: A user can download some data in CSV format from web site (RoR, if it matters). That CSV file is generated on the fly, sent to the user, and most of the time double-clicked and opened in MS Excel on Windows machine at the destination. Now, if the user has ',' set as the list separator, the data is properly arranged in columns, but if any other separator (';' is widely used here) is set, it all just gets thrown into a single column. So, is there any way to detect what separator is used on the client machine, and generate the file accordingly?

I have a sinking feeling that it is not, but I'd like to be sure before I pass the 'can't be done, sorry' line to the customer :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

超可爱的懒熊 2024-07-31 16:43:37

这是我刚刚根据所示方法编写的 JavaScript 解决方案 此处

function getListSeparator() {
    var list = ['a', 'b'], str;
    if (list.toLocaleString) {
        str = list.toLocaleString();
        if (str.indexOf(';') > 0 && str.indexOf(',') == -1) {
            return ';';
        }
    }
    return ',';
}

关键在于使用系统列表分隔符的toLocaleString()方法。

您可以使用 JavaScript 获取列表分隔符并将其设置在 cookie 中,然后您可以从服务器检测到该 cookie。

我检查了所有 Windows 区域设置,似乎默认列表分隔符实际上总是“,”或“;”。 对于某些区域设置,控制面板中的下拉列表提供这两个选项; 对于其他人来说,它只提供“,”。 一种语言环境 Divehi 有一个我以前从未见过的奇怪字符作为列表分隔符,并且对于任何语言环境,用户都可以输入他们想要的任何字符串作为列表分隔符。

将随机字符串作为 CSV 文件中的分隔符对我来说听起来很麻烦,因此上面的函数只会返回“;” 或“.”,并且它只会返回“;” 如果在 Array.toLocaleString 字符串中找不到“,”。 我不完全确定 array.toLocaleString 是否具有跨浏览器保证的格式,因此 indexOf 检查而不是挑选特定索引处的字符。

使用 Array.toLocaleString 获取列表分隔符适用于 IE6、IE7 和 IE8,但不幸的是它似乎不适用于 Firefox、Safari、Opera 和 Chrome(或者至少是我计算机上这些浏览器的版本):它们似乎都用逗号分隔数组项,无论 Windows“列表分隔符”设置如何。

另外值得注意的是,默认情况下,Excel 在解析 CSV 文件中的数字时似乎使用系统“小数分隔符”。 哎呀。 因此,如果您要本地化列表分隔符,您可能也需要本地化小数点分隔符。

Here's a JavaScript solution that I just wrote based on the method shown here:

function getListSeparator() {
    var list = ['a', 'b'], str;
    if (list.toLocaleString) {
        str = list.toLocaleString();
        if (str.indexOf(';') > 0 && str.indexOf(',') == -1) {
            return ';';
        }
    }
    return ',';
}

The key is in the toLocaleString() method that uses the system list separator.

You could use JavaScript to get the list separator and set it in a cookie which you could then detect from your server.

I checked all the Windows Locales, and it seems that the default list separator is virtually always either ',' or ';'. For some locales the drop-down list in the Control Panel offers both options; for others it offers just ','. One locale, Divehi, has a strange character that I've not seen before as the list separator, and, for any locale, it is possible for the user to enter any string they want as the list separator.

Putting random strings as the separator in a CSV file sounds like trouble to me, so my function above will only return either a ';' or a '.', and it will only return a ';' if it can't find a ',' in the Array.toLocaleString string. I'm not entirely sure about whether array.toLocaleString has a format that's guaranteed across browsers, hence the indexOf checks rather than picking out a character at a specific index.

Using Array.toLocaleString to get the list separator works on IE6, IE7, and IE8, but unfortunately it doesn't seem to work on Firefox, Safari, Opera, and Chrome (or at least the versions of those browsers on my computer): they all seem to separate array items with a comma, irrespective of the Windows "list separator" setting.

Also worth noting that by default Excel seems to use the system "decimal separator" when it's parsing numbers out of CSV files. Yuk. So, if you're localizing the list separator you might want to localize the decimal separator too.

鸩远一方 2024-07-31 16:43:37

getListSeparator 函数的简化版本,使任何字符都可以作为分隔符:

function getListSeparator_bis()
{
    var list = ['a', 'b'];
    return(list.toLocaleString().charAt(1));
}// getListSeparator_bis

只需在操作系统中将任何字符(fe '#')设置为列表分隔符,然后尝试上面的代码。 返回适当的字符(即“#”,如果设置为建议的)。

The simplier version of getListSeparator function, enabling any character to be a separator:

function getListSeparator_bis()
{
    var list = ['a', 'b'];
    return(list.toLocaleString().charAt(1));
}// getListSeparator_bis

Just set any char (f.e. '#') as list separator in your OS and try the code as above. The appropriate char (i.e. '#' if set as sugested) is returned.

浪漫之都 2024-07-31 16:43:37

我认为每个人都应该使用 OpenOffice 中的 Calc - 当您打开文件时它会询问有关编码、列分隔符等的信息。 我不知道你的问题的答案,但也许你可以尝试在 html 表或 xml 中发送数据 - excel 应该正确读取它们。 根据我的经验,将数据导出到 Excel 并不容易。 几周前,我遇到了问题,经过几个小时的工作,我向一个无法在 Excel 中打开我的 csv 文件的人询问版本。 这是 Excel 98...

查看 html 示例xml

I think everyone should use Calc from OpenOffice - it asks when you open a file about encoding, column separators and other. I don't know answer for your question, but maybe you can try to send data in html tables or in xml - excel should read both of them correctly. From my experience it isn't easy to export data to excel. Few weeks ago I have problem with it and after few hours of work I asked a person, who couldn't open my csv file in excel, about version. It was Excel 98...

Take a look on html example and xml.

撩人痒 2024-07-31 16:43:37

您能否让使用非逗号分隔符的用户设置配置文件类型的选项,然后根据用户设置生成 CSV(默认为逗号)?

Could you just have the users with non comma separators set a profile kind of option and then generate CSVs based on user settings with the default being commas?

放赐 2024-07-31 16:43:37

汤姆斯,据我所知,没有办法实现你所追求的目标。 您最多可以尝试检测用户区域设置并将其映射到区域设置/列表分隔符的数据库,从而更改 .CSV 文件中的列表分隔符。

Toms, as far as I'm aware there is no way of achieving what you're after. The most you can do is try and detect the user locale and map it against a database of locales/list separators, altering the list separator in the .CSV file as a result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文