Google Apps脚本中的字符串平等

发布于 2025-01-29 15:36:36 字数 592 浏览 5 评论 0原文

我正在尝试比较从JSON对象中检索的名称,并在Google表上使用该名称。尽我所能,我无法获得带来积极的比较。 我已经尝试过:

indexof

localecompare

==

=====

我尝试过 键===值 字符串(键)=== String(value)string(键).valueof()=字符串(value).valueof。 我还尝试在所有内容上调用trim()以确保没有领先/拖延的白色空间(没有,如length()比较所确认 从屏幕截图中可以看到,

value的值完全相同。 任何指针都会非常感激。这已经使我的项目持续了好几天!

屏幕截图

I'm trying to compare a name retrieved from a JSON object, with a name as it exists on a google Sheet. Try as I might, I can't get a comparison that yields a positive.
I've tried:

IndexOf

localeCompare

==

===

I've tried
key===value
String(key)===String(value) and
String(key).valueof()=String(value).valueof.
I've also tried calling trim() on everything to make sure there are no leading/trailing white spaces (there aren't, as confirmed with a length() comparison.

As you can see from the screen shot, the values of key and value are exactly the same.
Any pointers would be gratefully received. This has held up my project for days!

Screenshot here

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

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

发布评论

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

评论(2

风为裳 2025-02-05 15:36:36

描述

这不是解决方案,但可能有助于找到问题。也许一个或另一个角色不是您的想法。在视觉上它们比较,但是如果一个人有一个标签而不是空间,该怎么办。尝试一下,列出每个字符代码,看看任何字符是否具有不同的值。

我添加了另一个选项,该选项可借助@themaster

脚本(选项1)

function test() {
  try {
    let text = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test").getRange("A1").getValue();
    console.log(text);
    let code = [];
    for( let i=0; i<text.length; i++ ) {
      code.push(text.charCodeAt(i))
    }
    console.log(code.join());
  }
  catch(err) {
    console.log(err);
  }
}

脚本(选项2)

function test() {
  try {
    let text = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test").getRange("A1").getValue();
    console.log(text);
    let code = [];
    [...text].forEach( char => code.push(char.charCodeAt(0)) );
    console.log(code.join());
  }
  catch(err) {
    console.log(err);
  }
}

执行日志

7:57:53 AM  Notice  Execution started
7:57:56 AM  Info    Archie White
7:57:56 AM  Info    65,114,99,104,105,101,32,87,104,105,116,101
7:57:54 AM  Notice  Execution completed

Description

This is not a solution but it might help find the problem. Perhaps the characters in one or the other is not what you think. Visually they compare, but what if one has a tab instead of a space. Try this, list the character codes for each and see if any character has a different value.

I've added another option that eliminates the for loop thanks to @TheMaster

Script (Option 1)

function test() {
  try {
    let text = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test").getRange("A1").getValue();
    console.log(text);
    let code = [];
    for( let i=0; i<text.length; i++ ) {
      code.push(text.charCodeAt(i))
    }
    console.log(code.join());
  }
  catch(err) {
    console.log(err);
  }
}

Script (Option 2)

function test() {
  try {
    let text = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test").getRange("A1").getValue();
    console.log(text);
    let code = [];
    [...text].forEach( char => code.push(char.charCodeAt(0)) );
    console.log(code.join());
  }
  catch(err) {
    console.log(err);
  }
}

Execution log

7:57:53 AM  Notice  Execution started
7:57:56 AM  Info    Archie White
7:57:56 AM  Info    65,114,99,104,105,101,32,87,104,105,116,101
7:57:54 AM  Notice  Execution completed
维持三分热 2025-02-05 15:36:36

比较表

function compare() {
  const json = '{"values":[["good","bad","ugly"]]}';
  const obj = JSON.parse(json);
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet0");
  const [hA,...vs] = sh.getDataRange().getDisplayValues();
  let matches = [];
  vs.forEach((r,i) => {
    r.forEach((c,j) => {
      let idx = obj.values[0].indexOf(c);
      if(~idx) {
        matches.push({row:i+1,col:j+1,index: idx});
      }
    })
  })
  Logger.log(JSON.stringify(matches))
 }

Execution log
9:31:50 AM  Notice  Execution started
9:31:52 AM  Info    [{"row":1,"col":1,"index":0},{"row":2,"col":1,"index":1},{"row":3,"col":1,"index":2}]
9:31:51 AM  Notice  Execution completed

col1
好坏
1

Compare

function compare() {
  const json = '{"values":[["good","bad","ugly"]]}';
  const obj = JSON.parse(json);
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet0");
  const [hA,...vs] = sh.getDataRange().getDisplayValues();
  let matches = [];
  vs.forEach((r,i) => {
    r.forEach((c,j) => {
      let idx = obj.values[0].indexOf(c);
      if(~idx) {
        matches.push({row:i+1,col:j+1,index: idx});
      }
    })
  })
  Logger.log(JSON.stringify(matches))
 }

Execution log
9:31:50 AM  Notice  Execution started
9:31:52 AM  Info    [{"row":1,"col":1,"index":0},{"row":2,"col":1,"index":1},{"row":3,"col":1,"index":2}]
9:31:51 AM  Notice  Execution completed

Sheet1:

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