javascript 如何使用应用程序脚本重现谷歌表单左功能

b1uwtaje  于 2023-09-29  发布在  Java
关注(0)|答案(1)|浏览(95)

我在电子表格的第二行有40个不同的数据单元格。所有数据都以2个字母的代码开始,然后是一个空格,然后是一些其他信息。我希望能够提取字符串的前两个字母,并将其放在其正上方的行中。我可以使用什么代码来实现这一点?

2nbm6dog

2nbm6dog1#

function usingFunction(){
 var sheetName         = 'Sheet1';  //change according to your need 
 var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
 var selectedSheet     = activeSpreadsheet.getSheetByName(sheetName); 
 var mySheet           = selectedSheet;
 var lastRow           = mySheet.getLastRow();
 var N                 = lastRow;

for (var i=2;i<=N;i++){// taking i=2 for headers change accordingly

var left           ='LEFT($A'+i+',2)'; //Change "$A"according to your cell reference and "2" by the characters you need

mySheet.getRange(i, 2, 1, 1).setFormula(left);//sets formulas 

//pasting it as values keep if needed

var getB =mySheet.getRange(i, 2, 1, 1);
var valueB  ="B"+i;
getB.copyTo(mySheet.getRange(valueB), {contentsOnly:true});
}}

相关问题