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 GoogleSheets查找和替换脚本_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps script GoogleSheets查找和替换脚本

Google apps script GoogleSheets查找和替换脚本,google-apps-script,google-sheets,Google Apps Script,Google Sheets,所以我一直在玩弄这两个版本的查找和替换脚本。我遇到的问题是,脚本的任何连续使用都会删除以前的使用。有人提到我,但我要么实施错误,要么它们不是所需的修复 如果可能的话,我想继续使用脚本,而不是一次性查找和替换 这绝对不是最好的,如果我在这里容易出错,很抱歉!谢谢:) 函数runReplaceInSheet(){ var sheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“下层”); //以数组形式获取当前数据范围值 //访问工作

所以我一直在玩弄这两个版本的查找和替换脚本。我遇到的问题是,脚本的任何连续使用都会删除以前的使用。有人提到我,但我要么实施错误,要么它们不是所需的修复

如果可能的话,我想继续使用脚本,而不是一次性查找和替换

这绝对不是最好的,如果我在这里容易出错,很抱歉!谢谢:)

函数runReplaceInSheet(){
var sheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“下层”);
//以数组形式获取当前数据范围值
//访问工作表的调用更少->开销更低
var values=sheet.getDataRange().getValues();
//替换员工姓名
替换图纸(值,/^T$/,'=图像(“https://i.imgur.com/Dxl893F.png")');
替换图纸(值,/^A$/,'=图像(“https://i.imgur.com/omc7F9l.png")');
替换图纸(值,/^R$/,'=图像(“https://i.imgur.com/12ZmSp3.png")');
替换图纸(值,/^M$/,'=图像(“https://i.imgur.com/kh7RqBD.png")');
替换图纸(值,/^H$/,'=图像(“https://i.imgur.com/u0O7fsS.png")');
替换图纸(值,/^F$/,'=图像(“https://i.imgur.com/Hbs3TuP.png")');
替换图纸(值,/^t$/,'=图像(“https://i.imgur.com/Dxl893F.png")');
替换图纸(值,/^a$/,'=图像(“https://i.imgur.com/omc7F9l.png")');
替换图纸(值,/^r$/,'=图像(“https://i.imgur.com/12ZmSp3.png")');
替换图纸(值,/^m$/,'=图像(“https://i.imgur.com/kh7RqBD.png")');
替换图纸(值,/^h$/,'=图像(“https://i.imgur.com/u0O7fsS.png")');
替换图纸(值,/^f$/,'=图像(“https://i.imgur.com/Hbs3TuP.png")');
//一次将所有更新的值写入工作表
sheet.getDataRange().setValues(值);
}
函数替换表(值,要替换,替换为){
//循环数组中的行
for(值中的var行){
//使用Array.map对行中的每个单元格执行替换调用。
var替换的_值=值[行].map(函数(原始_值){
返回原始的_值.toString().replace(要_replace,请将_替换为);
});
//用替换的值替换原始行值
值[行]=替换的值;
}
}
  • 您希望在脚本运行时保留现有值(图像),以替换新值
如果我的理解是正确的,这次修改怎么样?在这次修改中,我在您的问题中使用了下面的脚本。请修改如下

修改点:
  • 在此修改中,首先检索值和公式。对于具有公式的单元格,将使用相同的公式。对于具有值的单元格,运行替换脚本并放置替换的公式
  • 通过上述流程,在本次修改中,使用了
    setFormulas()
    而不是
    setValues()
修改脚本: 发件人: 致: 参考资料:

如果我误解了你的问题,而这不是你想要的结果,我很抱歉。

这很有效,非常感谢!它保留了现有的图像!然而,它试图替换所有包含这些字母的内容。而不是独立的T、a等。单元格内的文本要么=#错误!或者=#姓名?@Ryan King感谢您的回复。我很高兴你的问题解决了。我愿意支持你。但你评论的问题是新问题,这与你的问题不同。那么,你能通过包含详细信息将其作为新问题发布吗?因为当你的初始问题被评论改变时,看到你的问题的其他用户会感到困惑。通过将其作为新问题发布,包括我在内的用户可以想到您的新问题。当时,。如果您能合作解决您的新问题,我很高兴。@例如,Ryan King,我认为使用
getRange()
而不是
getDataRange()
来使用所需的数据范围。但不幸的是,我认为很难决定这是否能解决你的新问题,因为信息还不够。
range.setValues(
  range.getValues().map(function(row) {
    return row.map(function(original_value) {
      return original_value.toString().replace(regex, replacer);
    });
  })
);

var data = range.getValues();
data = range.getFormulas().map(function(e, i) {//i=index of row(e)
  return e.map(function(f, j) {//j = index of column(f)
    return f === "" ? data[i][j] : f;
  });
});
// This part is not used in this modification.
// range.setValues(
//   range.getValues().map(function(row) {
//     return row.map(function(original_value) {
//       return original_value.toString().replace(regex, replacer);
//     });
//   })
// );

var data = range.getValues();
range.setFormulas(
  range.getFormulas().map(function(e, i) {//i=index of row(e)
    return e.map(function(f, j) {//j = index of column(f)
      return f === "" ? data[i][j].toString().replace(regex, replacer) : f;
    });
  })
);