Google表格Scrips getFormula帮助

时间:2015-05-06 17:42:51

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

我一直在使用Google表格中的小型搜索工具。这是我创建的一个项目,用于了解GS,并希望将我的一些学习成果付诸实践。

在过去的几周里,Munkey一直在帮助我发展我的技能和理解。

我已在“数据库”和搜索工具下方链接。

数据库 - https://docs.google.com/spreadsheets/d/1K53LOopwAJuOVPJ5RXgpmEO7L3JPHnW5Fx2qp6_3kqo/edit?usp=sharing

搜索工具 - https://docs.google.com/spreadsheets/d/1mY13e-75dBYfKgkjV8dFFFEvxC838nGNxPrUdusc0PA/edit?usp=sharing

我将下面的脚本“推送”数据从数据库“推送”到Vlookup函数使用的搜索工具数据库。

function getOvenDataV4(){

  var ui = SpreadsheetApp.getUi();

  ui.createMenu('Export Data')
      .addItem('Export Oven Data Now', 'getOvenDataV4')
      .addToUi();

  var settingsSheetName = "oven_settings";

  /* DO NOT EDIT BELOW THIS LINE */  


  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var settings = ss.getSheetByName(settingsSheetName); // this loads the settings sheet
  var targetSheet = settings.getRange("C2").getValue(); // this gets the     target sheet name from the settings sheet

  var sSheets = settings.getRange("B2:B").getValues(); // this gets the     values in column B in settings, the source sheet names
  var sourceSheets = []; // this is where we will store the sheet names once     we make sure they are not blank


  // this loops makes sure that there is data in Column B in the settings     sheet, if there is, push it to sourceSheets Array
  for(var i = 0; i < sSheets.length;i++){
    if(sSheets[i][0]!=""){
       sourceSheets.push(sSheets[i]);
    }
  }

var dKeys = settings.getRange("A2:A").getValues(); // This gets the values     of column A in settings, the spreadsheet keys/IDs
var sKeys = []; // this is where we will store the ID's/keys that are not     blank for later


  // this loop makes sure that column A, the spreadsheet IDs/keys,not     blank, if they are not blank. Lets push them into the sKeys Array
for(var i = 0; i < dKeys.length;i++){
  if(dKeys[i][0]!=""){
    sKeys.push(dKeys[i]);
 }

}

var data = []; 

for (var i= 0; i<sourceSheets.length;i++){

  var values = ss.getSheetByName(sourceSheets[i]).getDataRange().getValues();     

  for (var x = 1;x < values.length; x++){
      if(values[x][0]!= ""){
        data.push(values[x]);
      }
  }
}

  // below loops through all your keys, opens that sheet by ID, which we have, and opens the target sheet, which we have and writes the data
for (var i = 0; i< sKeys.length;i++){
    var tss = SpreadsheetApp.openById(sKeys[i]);
    var target =  tss.getSheetByName(targetSheet);
    var range = target.getRange(2,1, data.length, data[0].length);    target.getRange(2,1,target.getLastRow()-1,target.getLastColumn()).clearContent(); 
    range.setValues(data); 
  }
}

Munkey发表了一些评论,这些评论被添加到上面的snippit中,以帮助我理解它是如何工作的。该脚本非常有效并且符合预期。但是,它不会从“H”列中的=image("")公式中提取数据。

任何人都可以提供有关如何调整上述脚本以将=Image("")公式提取到搜索工具的任何帮助吗?与=importrange的做法类似吗?

我尝试使用getFormulagetFormulas方法搜索此内容,包括google开发网站以及各种不同的方法,但到目前为止似乎没有任何工作。我很可能只是错误地使用它们:)

1 个答案:

答案 0 :(得分:0)

这可能是一个笨拙的解决方案,但我认为它有效。 它创建一个名为row的数组,并以您需要的方式排列数据。 将值和公式都添加到行数组中。

非常愿意接受有关如何改善这一点的建议。

function getOvenDataV6() {

  var settingsSheetName = "monkey_settings";

  /* DO NOT EDIT BELOW THIS LINE */

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var settings = ss.getSheetByName(settingsSheetName);

  // this bit has been edited, note the getValues, not getValue, as we want the whole column now not just a single cell.
  var targetSheetsValues = settings.getRange("C2:C").getValues(); // this gets the target sheet names from the settings sheet
  var targetSheets = []; // And array added to throw target sheet names into, as there is more than one.

  // the reason we use arrays and loops (later on), is because the script has no idea how much data to expect.
  // so we go through whatever it's grabbed, the stuff it thinks is data, but we check it later on.
  // only a simple check. Our check is that it cannot be blank. ""
  // then stuff it in an array, a handy thing to store data, for use later on.

  var sSheets = settings.getRange("B2:B").getValues();
  var sourceSheets = [];

  // new loop below to get the target sheets names. We'll use this in the write bit later.

  for (var i = 0; i < targetSheetsValues.length; i++) {
    if (targetSheetsValues[i][0] != "") {
      targetSheets.push(targetSheetsValues[i]);
    }

  }

  for (var i = 0; i < sSheets.length; i++) {
    if (sSheets[i][0] != "") {
      sourceSheets.push(sSheets[i]);
    }

  }

  var dKeys = settings.getRange("A2:A").getValues();
  var sKeys = [];

  for (var i = 0; i < dKeys.length; i++) {
    if (dKeys[i][0] != "") {
      sKeys.push(dKeys[i]);
    }

  }

  var data = [];
  for (var i = 0; i < sourceSheets.length; i++) {

    // below has changed from tss.getSheetByName() to ss.getSheetByName() as we are reading data from local sheeet

    var values = ss.getSheetByName(sourceSheets[i]).getDataRange().getValues();
    var formula = ss.getSheetByName(sourceSheets[i]).getDataRange().getFormulas();

    for (var x = 1; x < values.length; x++) {
      var row = [
        [values[x][0]],
        [values[x][1]],
        [values[x][2]],
        [values[x][3]],
        [values[x][4]],
        [values[x][5]],
        [values[x][6]],
        [formula[x][7]]
      ];

      if (values[x][0] != "") {
        data.push(row);
      }

    }

  }

  // Below is an array of your column headers, the script was being annoying when clearing sheet data, so decided to clear the whole damn sheet
  // then write the headers via here instead
  var headers = [
    ["Model No", "Product Code", "Brand", "Model No", "kW", "Amp", "Apeture", "Image"]
  ];

  for (var i = 0; i < sKeys.length; i++) {
    var tss = SpreadsheetApp.openById(sKeys[i]);
    for (var x = 0; x < targetSheets.length; x++) { // this loop, within the keys loop, goes through the target sheets array
      var target = tss.getSheetByName(targetSheets[x]); // this loads the target sheet, one by one
      var range = target.getRange(2, 1, data.length, data[0].length); // this gets the cells to write to
      target.clearContents(); // clear the sheet before writing data
      target.getRange("A1:F1").setValues(headers); // write the headers to a1:F1 in target sheet

      range.setValues(data); //write the data
    }
  }
}

原始代码中唯一真正的变化就是这个。

 for (var i= 0; i<sourceSheets.length;i++){


// below has changed from tss.getSheetByName() to ss.getSheetByName() as we are reading data from local sheeet

    var values = ss.getSheetByName(sourceSheets[i]).getDataRange().getValues();
    var formula = ss.getSheetByName(sourceSheets[i]).getDataRange().getFormulas();



    for (var x = 1;x < values.length; x++){
      var row = [  [values[x][0]], [values[x][1]], [values[x][2]], [values[x][3]],[values[x][4]],[values[x][5]],[values[x][6]],[formula[x][7]]   ];     



      if(values[x][0]!= ""){
        data.push(row);
      }


  }




}