谷歌应用程序脚本电子表格字符串检索问题

时间:2018-01-25 21:34:27

标签: javascript google-apps-script utf-8 google-sheets google-drive-api

我无法弄清楚为什么以下代码在特殊字符的情况下无法正常工作:

[...]
// get strings (names) from spreadsheet
var persons = SpreadsheetApp.getActiveSheet().getRange(2, 1, 31, 4).getValues();

// for each row
for (var row in persons) {

    // build the filename
    var myFile = persons[row][1] + "_" + persons[row][0] + "_20180124.txt";

    [...]

    // handle from Google Drive only file             
    var driveFiles = DriveApp.getFilesByName(myFile);
    while (driveFiles.hasNext()) {
        var file = driveFiles.next();

        if(driveFiles.getName() == myFile) {

            /* write to Log

            Saša_KLANJŠČEK_20180124.txt     not written
            Peter_MARINČIČ_20180124.txt     not written
            Peter_KLANJŠČEK_20180124.txt    not written
            Niko_ČERNIC_20180124.txt        not written
            Tjaša_KOGOJ_20180124.txt        written
            */

            Logger.log(myFile + "\n");

        [...]

带有大写unicode字符的字符串会导致条件语句失败。我尝试使用toString(" UTF-8")方法,但它仍然无法正常工作。这是编码问题吗?

1 个答案:

答案 0 :(得分:0)

变化:

if(driveFiles.getName() == myFile) {

 if(file.getName() == myFile) {