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
Google apps script googlesheets脚本未正确格式化我的内容_Google Apps Script - Fatal编程技术网

Google apps script googlesheets脚本未正确格式化我的内容

Google apps script googlesheets脚本未正确格式化我的内容,google-apps-script,Google Apps Script,我的googlesheet脚本没有正确格式化我的内容。有人能帮我吗?这是我的剧本 function NewIssue() { var spreadsheet = SpreadsheetApp.getActive(); var cell = spreadsheet.getCurrentCell(); var user = Session.getActiveUser().getEmail().substring(0, 2).toUpperCase(); var oldContent

我的googlesheet脚本没有正确格式化我的内容。有人能帮我吗?这是我的剧本

function NewIssue() {
  var spreadsheet = SpreadsheetApp.getActive();
  var cell = spreadsheet.getCurrentCell();
  var user = Session.getActiveUser().getEmail().substring(0, 2).toUpperCase();
  var oldContent = cell.getValue();
  var newContent1 = '********************ISSUE********************\n';
  var newContent2 = 'Group:\n\nDescription:\n\nExpected Results:\n\nActual Results:\n\nTest Results:\n\nTest Data:\n\n\n';
  var space = " ";

  //Keep the format of the old content and add format to newContent1
  var newStyles = [{
    start: 0,
    end: newContent1.length,
    style: SpreadsheetApp.newTextStyle().setBold(true).setForegroundColor("red").build()
  }];
  var richTextValue = cell.getRichTextValue();
  var offset = newContent1.length + newContent2.length + space.length;
  var oldContent = richTextValue.getText();
  if (oldContent.length > 0) {
    richTextValue.getRuns().forEach(function(e) {
      newStyles.push({
        start: offset + e.getStartIndex(),
        end: offset + e.getEndIndex(),
        style: e.getTextStyle()
      });
    });
  }

  var d = new Date(); 
  var richText = SpreadsheetApp.newRichTextValue().setText(user + "_" + d.dateNow() + "_" + d.timeNow() + "\n" + newContent1 + newContent2 + space + oldContent);
  newStyles.forEach(function(e) {richText.setTextStyle(e.start, e.end, e.style)});
  cell.setRichTextValue(richText.build());

  Date.prototype.timeNow = function () 
{
     return ((this.getHours() < 10)?"0":"") + ((this.getHours()>12)?(this.getHours()-12):this.getHours()) +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() + ((this.getHours()>=12)?('pm'):'am');
}
// Returns the current date with formatting like: 6.16.19
Date.prototype.dateNow = function ()

{
     return (this.getMonth()+1 +"."+ this.getDate() +"."+ this.getYear().toString().substr(-2));
}   

}
函数NewIssue(){
var电子表格=SpreadsheetApp.getActive();
var cell=spreadsheet.getCurrentCell();
var user=Session.getActiveUser().getEmail().substring(0,2).toUpperCase();
var oldContent=cell.getValue();
var newContent1='*************************问题************************************\n';
var newContent2='组:\n\n说明:\n\n预期结果:\n\n实际结果:\n\n测试结果:\n\n测试数据:\n\n\n';
var space=“”;
//保留旧内容的格式,并将格式添加到新内容1
var newStyles=[{
起点:0,
结束:newContent1.length,
样式:SpreadsheetApp.newTextStyle().setBold(true).setForegroundColor(“红色”).build()
}];
var richTextValue=cell.getRichTextValue();
var offset=newContent1.length+newContent2.length+space.length;
var oldContent=richTextValue.getText();
如果(oldContent.length>0){
richTextValue.getRuns().forEach(函数(e){
新闻风格({
开始:偏移量+e.getStartIndex(),
结束:偏移量+e.getEndIndex(),
样式:e.getTextStyle()
});
});
}
var d=新日期();
var richText=SpreadsheetApp.newRichTextValue().setText(用户+“\u”+d.dateNow()+“\u”+d.timeNow()+“\n”+newContent1+newContent2+space+oldContent);
forEach(函数(e){richText.setTextStyle(e.start,e.end,e.style)});
cell.setRichTextValue(richText.build());
Date.prototype.timeNow=函数()
{
返回((this.getHours()<10)?“0”:“)+((this.getHours()>12)?(this.getHours()-12):this.getHours())+”:“+((this.getMinutes()<10)?“0”:“)+this.getMinutes()+((this.getHours()>=12)?('pm'):'am');
}
//返回当前日期,格式如下:6.16.19
Date.prototype.dateNow=函数()
{
return(this.getMonth()+1++“+this.getDate()++“+this.getYear().toString().substr(-2));
}   
}
运行上述脚本时,我得到以下结果:

我希望它看起来像这样:

我希望用户,日期,时间都是黑色和非粗体的 下午2:19.20:57

我希望***************************************************************************************问题是红色和粗体

我不知道该怎么做。任何建议都会有帮助。

  • 您希望将红色的前底色和粗体的文本样式仅指定给
    ************************************************************问题
    newContent1
  • 您不想将文本样式更改为
    newContent2
    user+“\u”+d.dateNow()+“\u”+d.timeNow()+“\n”
  • 您希望将单元格中的旧值添加到
    newContent2
    user+“\u”+d.dateNow()+“\u”+d.timeNow()+“\n”
    +
    newContent1
    +
    newContent2
如果我的理解是正确的,那么这个答案呢?请把这看作是几个可能的答案之一

修改点:
  • 在脚本中,
    newContent1
    的开始索引和结束索引不正确。因为
    user+“\u”+d.dateNow()+“\u”+d.timeNow()+“\n”
    是由
    setText(user+“\u”+d.dateNow()+“\u”+d.timeNow()+“\n”+newContent1+newContent2+space+oldContent)
    添加的。
    • 在此修改中,它将
      user+“”+d.dateNow()+“”+d.timeNow()+“\n”
      声明为
      newContent0
      。并设置每个值的开始索引和结束索引
    • 当您希望为除
      newContent1
      之外的值指定默认文本样式时,在这种情况下,需要指定
      null
      作为样式
当上述各点反映到脚本中时,它将变成如下所示

修改脚本:
  • var newContent0 = user + "_" + d.dateNow() + "_" + d.timeNow() + "\n\n";
    
  • 参考:
    如果我误解了你的问题,而这不是你想要的方向,我道歉

    var newContent0 = user + "_" + d.dateNow() + "_" + d.timeNow() + "\n";
    
    var newContent0 = user + "_" + d.dateNow() + "_" + d.timeNow() + "\n\n";