英文:
How to transform data type when trying to find a match using Google Sheets query language
问题
I'm trying to query for a specific row in a sheet by its content using this query in javascript:
我试图使用JavaScript中的此查询来按内容查询表格中的特定行:
const query = `SELECT * WHERE ${idColumn} = '${id}'`;
The problem is, sometimes the column seems to be a string, and sometimes it seems to be a number.
When a number, I would have to remove the single quotes from the id ${idColumn} = ${id}
问题是,有时列似乎是字符串,有时似乎是数字。当为数字时,我需要从${idColumn} = ${id}
中移除单引号。
The problem is that I don't know when the column is going to be a string or number,
I was hoping I could transform the data to string first.
Is that possible?
问题是我不知道列何时是字符串或数字,我希望能够首先将数据转换为字符串。这是否可能?
Something like:
类似于:
const query = `SELECT * WHERE TO_TEXT(${jobIdColumn}) = '${jobId}'`;
EDIT:
Here is an example file where column A is considered numbers by Google sheet, and column B is considered String:
https://docs.google.com/spreadsheets/d/1HYEuPr_1xqeCqL29ekuoSv1lZuVgfDUxpc8c1ZMYjUA/edit#gid=0
编辑:
这里是一个示例文件,其中Google表格将列A视为数字,将列B视为字符串:
https://docs.google.com/spreadsheets/d/1HYEuPr_1xqeCqL29ekuoSv1lZuVgfDUxpc8c1ZMYjUA/edit#gid=0
I can't use the same formula, to query both Columns,
I have to use single quotes on column B but none in column A
我不能使用相同的公式查询两列,我必须在列B中使用单引号,而在列A中不使用
=QUERY(A1:B7,"SELECT A WHERE A = 5555 ",-1)
=QUERY(A1:B7,"SELECT A WHERE B = 'C5555' ",-1)
My Workaround right now is to use OR
, but that doesn't seem right
我目前的解决方法是使用OR
,但这似乎不是正确的解决方法
SELECT * WHERE ${idColumn} = '${id}' OR ${idColumn} = ${id}
英文:
I'm trying to query for a specific row in a sheet by its content using this query in javascript:
const query = `SELECT * WHERE ${idColumn} = '${id}'`;
The problem is, sometimes the column seems to be a string, and sometimes it seems to be a number.
When a number, I would have to remove the single quotes from the id ${idColumn} = ${id}
The problem is that I don't know when the column is going to be a string or number,
I was hoping I could transform the data to string first.
Is that possible?
Something like:
const query = `SELECT * WHERE TO_TEXT(${jobIdColumn}) = '${jobId}'`
EDIT:
Here is an example file where column A is considered numbers by Google sheet, and column B is considered String:
https://docs.google.com/spreadsheets/d/1HYEuPr_1xqeCqL29ekuoSv1lZuVgfDUxpc8c1ZMYjUA/edit#gid=0
I can't use the same formula, to query both Columns,
I have to use single quotes on column B but none in column A
=QUERY(A1:B7,"SELECT A WHERE A = 5555 ",-1)
=QUERY(A1:B7,"SELECT A WHERE B = 'C5555' ",-1)
My Workaround right now is to use OR
, but that doesn't seem right
SELECT * WHERE ${idColumn} = '${id}' OR ${idColumn} = ${id}
答案1
得分: 1
Here's the translated content without the code part:
虽然我不确定我是否正确理解你的问题,但以下修改如何?
从:
const query = `SELECT * WHERE ${idColumn} = '${id}'`;
到:
const query = `SELECT * WHERE ${idColumn} = ${isNaN(id) ? `'${id}'` : Number(id)}`;
- 通过这个修改,当
id
是一个字符串时,该值将被单引号括起来。当id
是一个数字时,该值将不被括起来。 - 在这种情况下,假设
id
不为空。请注意这一点。
英文:
Although I'm not sure whether I could correctly understand your question, how about the following modification?
From:
const query = `SELECT * WHERE ${idColumn} = '${id}'`;
To:
const query = `SELECT * WHERE ${idColumn} = ${isNaN(id) ? `'${id}'` : Number(id)}`;
- By this modification, when
id
is a string, the value is enclosed by the single quotes. Whenid
is a number, the value is not enclosed. - In this case, it supposes that
id
is not null. Please be careful about this.
Testing:
<!-- begin snippet: js hide: true console: true babel: false -->
<!-- language: lang-js -->
const idColumn = "A";
["sample", 123].forEach(id => {
const query = `SELECT * WHERE ${idColumn} = ${isNaN(id) ? `'${id}'` : Number(id)}`;
console.log(query);
});
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论