Google apps script 将活动单元格复制到包含字符串的其他单元格
在GoogleSheets中,我试图创建一个脚本,该脚本将从活动单元格中获取值,并将该值粘贴到B列中包含字符串“HR”的任何单元格中。有什么想法吗?这还不错;你只需要围绕应用程序脚本和Javascript中的一些概念来思考就可以了。但首先让我们从天真的方法开始Google apps script 将活动单元格复制到包含字符串的其他单元格,google-apps-script,google-sheets,google-sheets-macros,Google Apps Script,Google Sheets,Google Sheets Macros,在GoogleSheets中,我试图创建一个脚本,该脚本将从活动单元格中获取值,并将该值粘贴到B列中包含字符串“HR”的任何单元格中。有什么想法吗?这还不错;你只需要围绕应用程序脚本和Javascript中的一些概念来思考就可以了。但首先让我们从天真的方法开始 function firstTry() { var activeSheet = SpreadsheetApp.getActiveSheet(); // whatever is open var activeCell = Spre
function firstTry() {
var activeSheet = SpreadsheetApp.getActiveSheet(); // whatever is open
var activeCell = SpreadsheetApp.getCurrentCell(); // this is a single-cell range
var activeCellValue = activeCell.getValue(); // could be a string, number, etc
// Now let's look in column B for stuff to change
for (var i = 1; i <= activeSheet.getLastRow(); i++) {
var cell = activeSheet.getRange("B" + i);
var val = cell.getValue();
var valStr = String(val); // We could have gotten a number
if (valStr.indexOf("HR") != -1) {
cell.setValue(activeCellValue);
}
}
}
函数firstTry(){
var activeSheet=SpreadsheetApp.getActiveSheet();//打开的任何内容
var activeCell=SpreadsheetApp.getCurrentCell();//这是一个单单元格范围
var activeCellValue=activeCell.getValue();//可以是字符串、数字等
//现在让我们在B栏中查找需要更改的内容
对于(var i=1;我录制了一个宏,但它实际上只给了我选择文件的部分。我太习惯Excel VBA了,所以我很难使用此脚本。非常感谢。我尝试了这两种方法,但都得到了相同的错误。Typeerror:找不到包含在对象中的函数。显然,它在您要查找“HR”的行中有什么想法吗?哦,没有,我忘了includes
比应用程序脚本使用的JS版本更新。一个较旧的字符串方法indexOf
应该可以工作。我已经编辑了我的答案。谢谢jjjjoe…我现在收到一个不同的错误。TypeError“无法从此行的未定义中读取属性“0”…var val=colBData[I][0];Doh,因为在基于零的数组中,您应该执行i
而不是i它工作得非常好!非常感谢您,尤其是对标签的解释
function improvement() {
var activeSheet = SpreadsheetApp.getActiveSheet(); // whatever is open
var activeCell = SpreadsheetApp.getCurrentCell(); // this is a single-cell range
var activeCellValue = activeCell.getValue(); // could be a string, number, etc
// Now let's look in column B for stuff to change
var rowsWithData = activeSheet.getLastRow() - 1;
var colBRange = activeSheet.getRange(1, // start on row 1
2, // start on column 2
rowsWithData, // this many rows
1); // just one column
// Let's get the data as an array of arrays. JS arrays are 0-based, btw
var colBData = colBRange.getValues();
for (var i = 0; i < colBData.length; i++) {
var val = colBData[i][0]; // row i, first column
var valStr = String(val); // We might have gotten a number
if (valStr.indexOf("HR") != -1) {
colBData[i][0] = activeCellValue; // modify copied data
}
}
// Lastly, write column B back out
colBRange.setValues(colBData);
}