Google apps script googlesheets脚本未正确格式化我的内容
我的googlesheet脚本没有正确格式化我的内容。有人能帮我吗?这是我的剧本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
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";