使用Google脚本切换工作表时出现问题

时间:2018-09-21 21:39:25

标签: google-apps-script google-sheets google-sheets-macros

我正在提交一个保存数据的表格。我想要一个脚本来提取数据,然后将这些数据归档到另一张纸中。
一栏包含我要查找的另一张纸的名称。我遍历数据查找正确的行,然后尝试选择该表。

    spreadSheetfile.getSheetByName(move_name);

move_name是一个我知道其中包含正确名称的字符串的变量。

总代码在这里。是的,这很丑陋,但是在切换工作表之前,实际上会将正确的数据放置在正确的位置。我通常会(因为我是一个低于平均水平的编码器)编写一些可行的方法,然后在情况变好时对其进行清理。

我可以在VBA中编写此代码。但是我正尝试切换为保留在Google Form / Sheet上。

function TestMacro() {

//set variables
  var spreadSheetfile = SpreadsheetApp.getActiveSpreadsheet();
  var specificsheet = spreadSheetfile.getSheets()[0];
  var counter = 2;
  var specificrange = specificsheet.getRange(counter, 2, 1,1);
  var Processed_value = specificrange.getValues();
  var responCE;
  var row_hold;
  var move_name = "String";
  var move_status;
  var move_now;
  var move_detail;
  var move_project;
  var sheetCOUNTER=0;
//find first unprocessed row
  while (Processed_value==1){
    counter = counter+1
    specificrange = specificsheet.getRange(counter, 2,1,1);
    Processed_value = specificrange.getValues();
  };
//save unprocessed row
  row_hold = counter;
//as long as not yet existing row keep going
  specificrange = specificsheet.getRange(row_hold, 1,1,1);
  Processed_value = specificrange.getValues();
  while (Processed_value!==""){
    specificrange = specificsheet.getRange(row_hold, 3,1,1);
    move_name = specificrange.getValues();
    specificrange = specificsheet.getRange(row_hold, 1,1,1);
    move_now=specificrange.getValues();
    //set up loop
    counter =4;
    specificrange = specificsheet.getRange(row_hold, counter ,1,1);
    move_project = specificrange.getValues();
    //find what data needs to be moved
    while(move_project==""){
      counter = counter+1;
      specificrange = specificsheet.getRange(row_hold, counter ,1,1);
      move_project = specificrange.getValues();
      if(move_project==""){}
      else{break};
    };
    //get the rest of the data together
    specificrange = specificsheet.getRange(row_hold, counter+1 ,1,1);
    move_status= specificrange.getValues();;
    specificrange = specificsheet.getRange(row_hold, counter+2 ,1,1);
    move_detail= specificrange.getValues();;
    //go to new sheet
    responCE = Browser.msgBox("8 Find new sheet", move_name, Browser.Buttons.OK_CANCEL);
    spreadSheetfile.getSheetByName(move_name);
    //find the right row
    counter =2;
    specificrange = specificsheet.getRange(counter,1,1,1)
    Processed_value = specificrange.getValues();
    responCE = Browser.msgBox("9 Find right row", Processed_value, Browser.Buttons.OK_CANCEL); 
    while(Processed_value!==""){
      //place the data
      if(Processed_value==move_project){
         specificrange = specificsheet.getRange(counter,2,1,1);
         specificrange.setValue(move_now);
         specificrange = specificsheet.getRange(counter,3,1,1);
         specificrange.setValue(move_status);
         specificrange = specificsheet.getRange(counter,4,1,1);
         specificrange.setValue(move_detail);
         move_detail = "";
        };
        counter = counter+1;
        specificrange = specificsheet.getRange(counter,1,1,1);
        Processed_value = specificrange.getValues();
        responCE = Browser.msgBox("10 Find right row", Processed_value, Browser.Buttons.OK_CANCEL); 
     };
    //new project make sure to add it
    if(move_detail!==""){
         specificrange = specificsheet.getRange(counter,1,1,1);
         specificrange.setValue(move_project);
         specificrange = specificsheet.getRange(counter,2,1,1);
         specificrange.setValue(move_now);
         specificrange = specificsheet.getRange(counter,3,1,1);
         specificrange.setValue(move_status);
         specificrange = specificsheet.getRange(counter,4,1,1);
         specificrange.setValue(move_detail);      
    };
    responCE = Browser.msgBox("11 Go back", "Form Responses 1", Browser.Buttons.OK_CANCEL); 

    //go back to original sheet
    spreadsheet.setActiveSheet(spreadsheet.getSheetByName("Form Responses 1"), true);
    specificsheet = spreadSheetfile.getSheets()[0];
    specificrange = specificsheet.getRange(row_hold, 2,1,1);    
    specificrange.setValue(1);
    row_hold = row_hold+1;
    specificrange = specificsheet.getRange(row_hold, 1,1,1);
    Processed_value = specificrange.getValues();
  };

  responCE = Browser.msgBox("12 Winnning!", "tiger blood", Browser.Buttons.OK_CANCEL);
  return;



    responCE = Browser.msgBox(Processed_value, counter, Browser.Buttons.OK_CANCEL);
    if(responCE=="cancel"){
    return;
    };
};

0 个答案:

没有答案