首页 > 解决方案 > 遍历谷歌表格中的单元格并创建具有特定格式的字符串

问题描述

在@Tanaike 的建议下,我重写了我的问题以使其更清楚。

我想处理一个谷歌表并创建一个特定值的字符串。例如,这是一个虚构的数据集, 在此处输入图像描述

上面的数据集中有 3 行,我想创建一个文本字符串,其内容如下所述:

在此处输入图像描述

这是最终结果: 在此处输入图像描述

这是上面所有图片的谷歌表格链接

感谢您的时间!

标签: google-apps-scriptgoogle-sheets

解决方案


在这里,我做了前三个字符串。您可以自己完成其余的工作。

function stupidStrings() {
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getActiveSheet();
  //const rg=sh.getRange("A1:T4");
  const rg=sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn);//perhaps this is the modification you are looking for as this will get all of the rows of data
  const vA=rg.getValues();
  var html="";
  html+=Utilities.formatString('<br />AIR***[%s][%s][%s]*99*****\\',vA[1][0],vA[1][1],vA[1][2]);
  html+=Utilities.formatString('<br />PAT*[%s]*[%s]****\\',vA[1][3],vA[1][5]);                             
  html+=Utilities.formatString('<br />PRE*[%s][%s][%s]*[%s]**\\',vA[1][12],vA[1][13],vA[1][7],vA[1][19]);
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html), 'Stupid Strings')
}

推荐阅读