You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Google Apps Script实现文本拆分及逐行检查至空白单元格?

Google Apps Script 实现逐行文本拆分直至空白行

以下是修改后的脚本,可实现从第3行开始逐行检查B-D列,直到遇到整行空白时停止,同时将每个单元格内容按/拆分后处理(两种版本可选,匹配不同需求):

版本1:拆分后保留最后一个非空白项

该版本会将每个单元格的内容按/拆分,过滤空白部分后,把最后一个有效内容写回原单元格:

function splitTextUntilBlankRow() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  let currentRow = 3;

  while (true) {
    // 获取当前行B、C、D列的内容并去除首尾空格
    const bContent = sheet.getRange(currentRow, 2).getValue().toString().trim();
    const cContent = sheet.getRange(currentRow, 3).getValue().toString().trim();
    const dContent = sheet.getRange(currentRow, 4).getValue().toString().trim();

    // 若当前行B-D全为空,终止循环
    if (!bContent && !cContent && !dContent) break;

    // 处理B列
    if (bContent) {
      const splitParts = bContent.split('/').filter(part => part.trim() !== '');
      if (splitParts.length > 0) {
        sheet.getRange(currentRow, 2).setValue(splitParts[splitParts.length - 1]);
      }
    }

    // 处理C列
    if (cContent) {
      const splitParts = cContent.split('/').filter(part => part.trim() !== '');
      if (splitParts.length > 0) {
        sheet.getRange(currentRow, 3).setValue(splitParts[splitParts.length - 1]);
      }
    }

    // 处理D列
    if (dContent) {
      const splitParts = dContent.split('/').filter(part => part.trim() !== '');
      if (splitParts.length > 0) {
        sheet.getRange(currentRow, 4).setValue(splitParts[splitParts.length - 1]);
      }
    }

    currentRow++;
  }
}

版本2:拆分后将所有非空白项展开写入对应列

如果需要将拆分后的所有有效内容依次写入对应列的连续行中,使用以下版本:

function splitTextUntilBlankRow() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  let currentRow = 3;

  while (true) {
    const bContent = sheet.getRange(currentRow, 2).getValue().toString().trim();
    const cContent = sheet.getRange(currentRow, 3).getValue().toString().trim();
    const dContent = sheet.getRange(currentRow, 4).getValue().toString().trim();

    if (!bContent && !cContent && !dContent) break;

    // 处理B列:拆分后写入连续行
    let bRows = 1;
    if (bContent) {
      const splitParts = bContent.split('/').filter(part => part.trim() !== '');
      bRows = splitParts.length;
      if (bRows > 0) {
        sheet.getRange(currentRow, 2, bRows, 1).setValues(splitParts.map(part => [part.trim()]));
      }
    }

    // 处理C列
    let cRows = 1;
    if (cContent) {
      const splitParts = cContent.split('/').filter(part => part.trim() !== '');
      cRows = splitParts.length;
      if (cRows > 0) {
        sheet.getRange(currentRow, 3, cRows, 1).setValues(splitParts.map(part => [part.trim()]));
      }
    }

    // 处理D列
    let dRows = 1;
    if (dContent) {
      const splitParts = dContent.split('/').filter(part => part.trim() !== '');
      dRows = splitParts.length;
      if (dRows > 0) {
        sheet.getRange(currentRow, 4, dRows, 1).setValues(splitParts.map(part => [part.trim()]));
      }
    }

    // 跳至当前行处理后的下一个未处理行
    currentRow += Math.max(bRows, cRows, dRows);
  }
}

关键说明

  • 循环从第3行启动,逐行检查B-D列,当整行无有效内容时自动终止
  • 使用trim()和filter()过滤拆分后的空白内容,确保只处理有效文本
  • 版本1适合只保留最后一个有效拆分项的场景,版本2适合需要展开所有有效内容的场景

内容的提问来源于stack exchange,提问作者M Coder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 10:02:44