Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 在Google工作表中的单元格中循环并删除文本的子字符串_Google Apps Script_Google Sheets_Google Cloud Firestore - Fatal编程技术网

Google apps script 在Google工作表中的单元格中循环并删除文本的子字符串

Google apps script 在Google工作表中的单元格中循环并删除文本的子字符串,google-apps-script,google-sheets,google-cloud-firestore,Google Apps Script,Google Sheets,Google Cloud Firestore,我需要在我的Google工作表中遍历每个填充的单元格并删除这些字符:{stringValue=} 我的一个专栏如下所示: COLUMN NAME: category {stringValue=Pump} {stringValue=Motor} {stringValue=Motor} {stringValue=Flow Meter} {stringValue=Flow Meter} {stringValue=Flow Meter} {stringValue=Motor} {stringValue=

我需要在我的Google工作表中遍历每个填充的单元格并删除这些字符:
{stringValue=}

我的一个专栏如下所示:

COLUMN NAME: category
{stringValue=Pump}
{stringValue=Motor}
{stringValue=Motor}
{stringValue=Flow Meter}
{stringValue=Flow Meter}
{stringValue=Flow Meter}
{stringValue=Motor}
{stringValue=Flow Meter}
{stringValue=Pump}
{stringValue=Motor}
{stringValue=Pump}

我需要保留诸如马达或泵之类的文本

这是我填充工作表的方式:

function importData() {
  const firestore = getFirestore();

  const allDocuments = firestore.getDocuments('Data').map(function(document) {
    return document.fields;
  });

  const first = allDocuments[0];
  const columns = Object.keys(first);

  const sheet = SpreadsheetApp.getActiveSpreadsheet();
  const ss = sheet.getActiveSheet();
  ss.clear();
  sheet.appendRow(columns);

  allDocuments.forEach(function(document) {
    const row = columns.map(function(column) {
      return document[column];
    });
    sheet.appendRow(row);
  });
}

这可以使用脚本完成,也可以使用以下公式完成:

=LEFT(MID(A18,FIND("=",A18)+1,100),LEN(MID(A18,FIND("=",A18)+1,100))-1)
现在看来“=”总是在第13位,如果是这样,FIND()函数可以被删除,您可以硬编码13来简化该函数

您可以根据需要向下复制此公式,也可以使用数组公式,它将根据需要填充自身:

=ARRAYFORMULA(IF(A18:A <> "",LEFT(MID(A18:A,FIND("=",A18:A)+1,100),LEN(MID(A18:A,FIND("=",A18:A)+1,100))-1),""))
=ARRAYFORMULA(如果(A18:A)”,左(中间(A18:A,查找(“=”,A18:A)+1100),LEN(中间(A18:A,查找(“=”,A18:A)+1100))-1),“”)

您也可以通过查找/替换:

1.)选择整个目标列

2.)点击Ctrl-H打开查找/替换对话框

3.)在“查找”字段中输入以下内容:
^.+=(.+)。

4.)在“替换为”字段中输入以下内容:
$1

5.)选中“使用正则表达式搜索”框

6.)单击显示“全部替换”的按钮


整个过程大约需要10秒钟。

我相信你的目标如下

  • 您希望使用Google Apps脚本实现以下情况。。这些值放在一列中

    •   COLUMN NAME: category
        {stringValue=Pump}
        {stringValue=Motor}
        {stringValue=Motor}
        {stringValue=Flow Meter}
        {stringValue=Flow Meter}
        {stringValue=Flow Meter}
        {stringValue=Motor}
        {stringValue=Flow Meter}
        {stringValue=Pump}
        {stringValue=Motor}
        {stringValue=Pump}
      
    •   COLUMN NAME: category
        Pump
        Motor
        Motor
        Flow Meter
        Flow Meter
        Flow Meter
        Motor
        Flow Meter
        Pump
        Motor
        Pump
      
在这种情况下,我建议使用TextFinder。我认为当使用TextFinder时,您的目标可以以较低的处理成本实现您的目标。示例脚本如下所示

示例脚本:
  • 如果您想替换各种
    stringValue
    ,我认为您也可以使用
    \{.+\=(.++\}
    而不是
    \{stringValue\\=(.++\}

  • 关于你的新问题,
    当该字段中没有数据时,如何清除只包含{stringValue=}的单元格?
    ,在这种情况下,如何修改
    createTextFinder
    中的正则表达式,从
    \{stringValue\\=(.+)\}
    \{stringValue\=(.+)\}
    ?这样,
    {stringValue=}
    的值将替换为空值

结果: 使用上述脚本时,会出现以下情况

发件人:

致:

参考资料:

谢谢你。当该字段中没有数据时,如何清除仅包含
{stringValue=}
的单元格?@Ciaran Crowley感谢您的回复。关于你的新问题,我想建议将
createTextFinder
中的正则表达式从
\{stringValue\\=(.+)\}
修改为
\{stringValue\\\=(.+)\}
?这样,
{stringValue=}
的值将替换为空值。为此,我也更新了我的答案。如果我误解了你的新问题,我道歉。再次感谢你。与以前一样,您的解决方案工作得非常好。没有必要道歉。我在原始问题中给出的专栏恰好没有任何字段只包含
{stringValue=}
@Ciaran Crowley感谢您的回复和测试。我很高兴你的问题解决了。也谢谢你。
function myFunction() {
  const sheetName = "Sheet1"; // Please set the sheet name.
  const columnNumber = 1; // Please set the column number. 1 is the column "A".

  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  sheet
    .getRange(2, columnNumber, sheet.getLastRow() - 1, 1)
    .createTextFinder("\\{stringValue\\=(.+)\\}")
    .useRegularExpression(true)
    .replaceAllWith("$1");
}