获取脚本以处理特定工作表

时间:2013-10-21 20:21:15

标签: google-apps-script google-sheets

大家好,并提前致谢,

我有一个脚本,我想在每月定时触发器上设置,以复制Google电子表格中特定工作表上的B列。下面是脚本,但它只适用于当时打开的工作表。我需要添加什么才能让它在工作表的第一张表上工作?

function UpdateSummary() {
var sheet =SpreadsheetApp.getActiveSheet(); //define spreadsheet name
var column = sheet.getRange("O1").getValue();
var row = 2;
Utilities.sleep(100000);  // Slowdown the script (10 second)
var UpdateSummary = sheet.getRange("B2:B205"); // Select and copy the column
UpdateSummary.copyValuesToRange(sheet, column+3, column+3, row, row+205); // Paste the data
sheet.getRange(1, column+3).setValue(new Date()); // add date in column title
sheet.getRange("O1").setValue(column+1);  // Incremente variable
}

谢谢,

约翰

2 个答案:

答案 0 :(得分:0)

var sheet =SpreadsheetApp.getActiveSheet(); //define spreadsheet name

有了这种错误,就意味着被调用的函数不是你想要的函数。整个函数不正确或查看getActiveSheet()函数并编辑它以包含您当前不在的工作表。

答案 1 :(得分:0)

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet 1');   
  var values = sheet.getDataRange().getValues()  
  var maxRows = sheet.getLastRow()
  var datatoCopy = []   
  for( var row = values.length -1; row >= 0; --row ){
    if (values[row][5] == 'here'){
     var whereToCopy = row
     Logger.log(whereToCopy);
     break
     }
     }
   for(row=whereToCopy-1;row<maxRows-1;++row){
    var rowData=[]
    rowData.push(values[row+1][2]+'*C*');// I added these 'indicators' to show what happens... delete them when the result is ok ;-)
    rowData.push(values[row+1][3]+'*D*');//
    rowData.push(values[row+1][4]+'*E*');//
    datatoCopy.push(rowData);// store column data in an array - remove the ** that I used to see what was copied ;-)
    }
    Logger.log(datatoCopy)
sheet.getRange(whereToCopy, 3, datatoCopy.length, datatoCopy[0].length).setValues(datatoCopy);// overwrite data to column E
}`enter code here`