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]);
}
}
}