我对脚本完全陌生,需要一个用于 Apps 脚本的脚本来将单元格值重置为在另一组单元格中确定的值。
假设,当运行 Reset() 函数时,L9 应该更改为 M9 中的值。 此功能应同时在 L10 至 M10、L11 至 M11、L12 至 M12 处设置值。
任何帮助将不胜感激,并且解释一下每行正在做什么将非常有助于我学习!
我正在运行一个函数来减少 L 行值,如下所示:
function Fire1() {
const sheet = SpreadsheetApp.getActiveSheet();
const range = sheet.getRange("L9");
range.setValue(range.getValue()-1);
}
并尝试了不同的事情,比如
function Reset() {
const sheet = SpreadsheetApp.getActiveSheet();
const range = sheet.getRange("L9");
range.setValue(range.getValue("M9"));
}
再次几乎不明白发生了什么。
从
Say, when function Reset() is ran, L9 should change to the value in M9. This function should at the same time set value at L10 to M10, L11 to M11, and L12 to M12.
开始,我相信你的目标如下。
在您的显示脚本
Reset()
中,我认为range.setValue(range.getValue("M9"));
发生了错误。因为getValue
没有参数。
在这种情况下,下面的示例脚本怎么样?
在此示例中,使用了
copyTo
。
function Reset() {
const sheet = SpreadsheetApp.getActiveSheet();
// Retrieve range "M9".
const srcRange = sheet.getRange("M9");
// Retrieve range "L9:M12".
const dstRange = sheet.getRange("L9:M12");
// Copy cell value "M9" to "L9:M12".
srcRange.copyTo(dstRange, { contentsOnly: true });
}
运行此脚本时,“M9”的单元格值将复制到“L9:M12”单元格。
在此示例中,使用了
getValue
和 setValue
。
function Reset() {
const sheet = SpreadsheetApp.getActiveSheet();
// Retrieve range "M9".
const srcRange = sheet.getRange("M9");
// Retrieve range "L9:M12".
const dstRange = sheet.getRange("L9:M12");
// Copy cell value "M9" to "L9:M12".
dstRange.setValue(srcRange.getValue());
}
运行此脚本时,“M9”的单元格值将复制到“L9:M12”单元格。