Frage

I have been looking into other questions posted but haven't been able to understand how to apply the solutions to my situation. Hopefully an answer here will make it a lot more obvious to others also.

I have a function in Code.gs which copies rows of data (which meet a certain criteria) into another spreadsheet.

function CopyRowsToAUDistDataSheet() {
  var source = SpreadsheetApp.getActiveSpreadsheet();
  var target = SpreadsheetApp.openById("######");
  var lastRow = source.getLastRow();
  var source_sheet = source.getSheetByName("Raw Data");
  var target_sheet = target.getSheetByName("Distributors");

  var source_range = source_sheet.getDataRange();
  var target_range = target_sheet.getDataRange();

  var i = 2;
  while (i <= lastRow) {
  if (source_sheet.getRange("C"+i).getValue() == "Distributor" ) {
    var A = source_sheet.getRange("A"+i).getValue();
    var B = source_sheet.getRange("B"+i).getValue();
    var C = source_sheet.getRange("C"+i).getValue();
    var D = source_sheet.getRange("D"+i).getValue();
    var E = source_sheet.getRange("E"+i).getValue();
    var F = source_sheet.getRange("F"+i).getValue();
    var G = source_sheet.getRange("G"+i).getValue();
    var H = source_sheet.getRange("H"+i).getValue();
    var I = source_sheet.getRange("I"+i).getValue();
    var J = source_sheet.getRange("J"+i).getValue();
    var K = source_sheet.getRange("K"+i).getValue();
    var L = source_sheet.getRange("L"+i).getValue();
    var M = source_sheet.getRange("M"+i).getValue();
    var N = source_sheet.getRange("N"+i).getValue();
    N *= 100;
    var data = [A,B,C,D,E,F,G,H,I,J,K,L,M,N];
    target_sheet.appendRow(data);
    i++;
  } else {
    i++;
  }
}
}

Each time the data is copied across, updated values in the cells, OR updates made to the data from within the above function are not recognised (Note the addition of the "N *= 100" to multiply the value by 100 to fix an issue with percentages).

I am having trouble figuring out how to bypass this caching issue with the above function. I have tried passing it timestamps etc but no luck. I am sure I am missing something obvious here.

Any help will be greatly appreciated.

I have tried working with the solutions/answers posted here:

Refresh data retrieved by a custom function in google spreadsheet

Script to summarise data not updating

UPDATE:

The function is now updated to the following:

function CopyRowsTest() {
  var source = SpreadsheetApp.getActiveSpreadsheet();
  var target = SpreadsheetApp.openById("###");
  var source_sheet = source.getSheetByName("Raw Data");
  var target_sheet = target.getSheetByName("Distributors");
  var source_range = source_sheet.getDataRange();
  var source_data = source_range.getValues();

  for (var i = 1, r = source_data.length; i < r; i++) {
    if (source_data[i][2] === 'Distributor') {
      // Multiply % data by 100 to work around bug
      source_data[i][13] *= 100;
      source_data[i][16] *= 100;
      source_data[i][23] *= 100;
      source_data[i][29] *= 100;
      source_data[i][31] *= 100;

      target_sheet.appendRow(source_data[i]);
    }
  }
}
War es hilfreich?

Lösung

I have tested your script, and an functionally identical script in both new and old Google Spreadsheets. I can change the value of a cell in the Source sheet, run the script via the menu, then verify that the same data exists in the Destination sheet.

All of the caching issues listed involve custom functions run in a cell in the spreadsheet. Your script is only being run via a menu or the IDE when actively scripting, and would not meet the criteria for the recalculation issues listed.

You can use FILTER on an IMPORTRANGE, but that doesn't seem to be as 'live':

=FILTER(IMPORTRANGE("key", "Raw Data!A2:E100"),  ImportRange("key", "Raw Data!C2:C100") = "Distributor")

The script that follows is intended for old Google Spreadsheets and works reliably from the IDE or a Menu. Please let me know if it doesn't work for a small data set in two new sheets.

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [
    {name: "Copy Distributor Rows", functionName: "CopyRowsTest"}
  ];
  ss.addMenu("Copy Rows", menuEntries);
}

function CopyRowsTest() { 
  var source_ss = SpreadsheetApp.getActiveSpreadsheet();
  var target_ss = SpreadsheetApp.openById("xxx");

  var target_sheet = target_ss.getSheetByName("Distributors");
  var source_range = source_ss.getSheetByName("Raw Data").getDataRange();

  var source_values = source_range.getValues();
  var source_formats = source_range.getNumberFormats();

  // Filter the Values
  var filtered_values = source_values.filter(function (row) {

    // Filter your data here
    return row[2] === 'Distributor'; // Column C

  });

  // Append the filtered Values to the target sheet
  filtered_values.forEach(function (row, r) {

    // This will fix any % formatted columns caused by issue 1265
    row = fixPercentFormattedColumns(row, source_formats[r]);

    // Write data to Sheet
    target_sheet.appendRow(row);
  });
}

function fixPercentFormattedColumns(row, rowFormat) {
  return row.map(function (cell, colNum) {
    // Check that the format ends with %, if so return value * 100
    return /%$/.test(rowFormat[colNum]) ? cell * 100 : cell;
  });
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top