英文:
Set cell background of cell after value is appended
问题
以下是您要的代码部分的中文翻译:
function copynewentry() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var idstoadd = ss.getSheetByName("Resources");
var fulllist = ss.getSheetByName("Daily");
var tag = idstoadd.getRange('D2');
Logger.log("选项#1 - 开始")
// 步骤1 - 获取目标表(Full List)上的最后一行
var last_row = fulllist.getRange(1, 1, fulllist.getLastRow(), 1).getValues().filter(String).length;
Logger.log("步骤#1 - Full List 上的最后一行 = " + last_row)
// 步骤2 - 创建目标范围(或至少是目标的左上角)
var targetrange = fulllist.getRange(last_row + 1, 1);
Logger.log("步骤#2 - 目标范围将是 " + targetrange.getA1Notation())
// 步骤3 - 创建源范围
// 头部行数
var numheaderrows = 0
// 所以范围将是...
// var sourcerange = idstoadd.getRange(1 + numheaderrows, 1, idstoadd.getLastRow() - numheaderrows, 1);
var sourcerange = idstoadd.getRange('D2');
Logger.log("步骤#3 - 源范围 = " + sourcerange.getA1Notation());
// 步骤4 - 从源范围复制到目标范围
// 方法 = range.copyto
// 因此从源范围复制到目标范围
sourcerange.copyTo(targetrange, { contentsOnly: true });
Logger.log("步骤#4 - 复制源范围到目标范围并刷新");
tag.setValue(tag.getValue() + 1);
lastRowOfCol(1); // 输入要用作搜索基础的列号
// 步骤6 - 应用所有未处理的更改。
SpreadsheetApp.flush();
Logger.log("步骤#6 - 刷新电子表格")
Logger.log("选项#1 - 完成")
return false;
}
function lastRowOfCol(column) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var total = sheet.getMaxRows();
var values = sheet.getRange(1, column, total).getValues();
for (var i = total - 1; values[i] == "" && i > 0; i--) {}
var last = sheet.getRange(i + 1, column);
var range = sheet.getRange(i + 1, 1, 1, sheet.getLastColumn());
sheet.setActiveRange(range);
const bgColor = last.getBackground();
last.setBackground('cyan');
Utilities.sleep(2000);
last.setBackground(bgColor);
}
希望这有所帮助。如果您有其他问题或需要进一步的协助,请随时告诉我。
英文:
I have a script that is appending the value in cell D2 then adding one to that cell, basically stacking a list of numbers. To make it more user friendly, I want to activate the latest cell (which works), but I also want to set the background color to cyan for 2 seconds so the user knows exactly where to go.
I have it close, but instead of seeing the cyan color, I'm just seeing the row activate.
Any thoughts?
function copynewentry() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var idstoadd = ss.getSheetByName("Resources");
var fulllist = ss.getSheetByName("Daily");
var tag = idstoadd.getRange('D2');
Logger.log("Option#1 - Start")
// step 1 - get the last row on the target: Full List
var last_row = fulllist.getRange(1, 1, fulllist.getLastRow(), 1).getValues().filter(String).length;
Logger.log("Step#1 - the last row on fulllist = "+last_row)
// step 2 - create a target range (or at least the top left corner of the target)
var targetrange = fulllist.getRange(last_row+1,1);
Logger.log("Step#2 - the target range will be "+targetrange.getA1Notation())
// step 3 - create a source range
// how many header rows
var numheaderrows = 0
// so the range will be...
// var sourcerange = idstoadd.getRange(1+numheaderrows, 1, idstoadd.getLastRow()-numheaderrows, 1);
var sourcerange = idstoadd.getRange('D2');
Logger.log("Stewp#3 - the sourcerange = "+sourcerange.getA1Notation());
// step4 - copy from source to target
// method = range.copyto
// so copyto from the source range to the target range
sourcerange.copyTo(targetrange,{contentsOnly:true});
Logger.log("Step#4 - copied the source range to the target range and flushed");
tag.setValue(tag.getValue() + 1);
lastRowOfCol(1); //Enter the column number you want to use as the base of the search
// step 6 - apply all the pending changes.
SpreadsheetApp.flush();
Logger.log("Step#6 - Flushed the spreadsheet")
Logger.log("Option#1 - Completed")
return false;
}
function lastRowOfCol(column) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var total = sheet.getMaxRows();
var values = sheet.getRange(1, column, total).getValues();
for (var i = total - 1; values[i] == "" && i > 0; i--) {}
var last = sheet.getRange(i + 1, column);
var range = sheet.getRange(i + 1, 1, 1, sheet.getLastColumn());
sheet.setActiveRange(range);
const bgColor = last.getBackground();
last.setBackground('cyan');
Utilities.sleep(2000);
last.setBackground(bgColor);
}
答案1
得分: 1
From:
last.setBackground('cyan');
Utilities.sleep(2000);
To:
last.setBackground('cyan'');
SpreadsheetApp.flush(); // 已添加
Utilities.sleep(2000);
已添加:
关于以下额外问题,
> 我是否应该在代码中多次添加 spreadsheetapp.flush'?我认为只在最后添加一个会使代码执行每个操作,而不会在上面的操作完成之前继续。这不正确吗?
我认为不正确。
例如,当您希望在脚本运行期间显示当前情况时,需要使用 SpreadsheetApp.flush()
。当您的脚本用作测试时,当使用以下修改时,
last.setBackground('cyan');
SpreadsheetApp.flush(); // 已添加
Utilities.sleep(2000);
last.setBackground('red');
Utilities.sleep(2000);
last.setBackground(bgColor);
红色单元格无法看到。另一方面,当使用以下修改时,
last.setBackground('cyan');
SpreadsheetApp.flush(); // 已添加
Utilities.sleep(2000);
last.setBackground('red');
SpreadsheetApp.flush(); // 已添加
Utilities.sleep(2000);
last.setBackground(bgColor);
可以看到红色单元格。
英文:
In your script, how about the following modification?
From:
last.setBackground('cyan');
Utilities.sleep(2000);
To:
last.setBackground('cyan');
SpreadsheetApp.flush(); // Added
Utilities.sleep(2000);
Added:
About the following additional question,
> should I be adding multiple spreadsheetapp.flush's throughout my code? i thought just having one at the end sets the code to do every action and not proceed unless the action above is completed. is that not correct?
I think that it is No.
For example, when you want to show the current situation during the script is running, it is required to use SpreadsheetApp.flush()
. When your script is used as a test when the following modification is used,
last.setBackground('cyan');
SpreadsheetApp.flush(); // Added
Utilities.sleep(2000);
last.setBackground('red');
Utilities.sleep(2000);
last.setBackground(bgColor);
the red color cell cannot be seen. On the other hand, when the following modification is used,
last.setBackground('cyan');
SpreadsheetApp.flush(); // Added
Utilities.sleep(2000);
last.setBackground('red');
SpreadsheetApp.flush(); // Added
Utilities.sleep(2000);
last.setBackground(bgColor);
the red color cell can be seen.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论