2016-09-16 96 views
0

我有40個Google工作表文件,我需要更改標題顏色。他們在第1列和第13列有不同的行。我需要找到他們在哪裏,並改變他們的顏色。通過循環播放會減慢我的google appscript的速度。我如何加快速度?

這工作,痛苦緩慢。無論如何,我可以更有效地寫這篇文章嗎?我感覺像循環行到57(我知道是最後一行可能的標題)正在放慢我的過程,但我想不出有什麼更好的辦法。

function colorchange() { 
    // The following defines the variable SpreadsheetIDs, which is a list of all the ID's that are going to be changed 
     var spreadsheetIDs = ["1TZK5s3KvAFn9AMgWjIyPFAkjvhwL-3sGy5ieipQV-R8", //template 
        "1NSctLmGT0eWzvSj7P_NOUmgR79nIIokEEiaTJo6_Bb8", //1 
        "1plfjNQx4aFFnwcqMfv_wohbiYnlC_NVjW-etBM-xm5c", //2 
        "1i7myzXNFiFM6ZtkG590Hg4oEr-cAV8gsWxlV7rjL2b8", //3 
        "1-3r7D6nsVLT1FEZhGpGP9eF9G9HUZmuEe22aDajNBA0", //4 
        "1LBob3F3D1E__K6CJyBBIssho_cLkxm9uD00DP03QQz8", //5 
        "1Bot9TAbAr-g_Dgbqi4WJvXfE-Gt9t2ErxJk3Y-pvWZs", //6 
        "18kOx3-Q0DQhAA7jm19UZQhBUYShYVORWlQIgU_iicq4", //7 
        "1aHZ3FHUW6jt5HpuYb0fpBvfgTqSazXzZwJbvQEa8Qcs", //8 
        "1WkDHWL6sIkSu9qt_nizzXm2er-hxzuXEf8GsCTq3fug", //9 
        "1ZLDjW3VTMaumQIbihvyELyCuBghcn_GrQmuzCPnno9Y", //10 
        //ECT until 40ish spreadsheetID's 
        ]; 
     var i=0,color="",sheet,thisID="",e=1,c=1; 

    // This calls the Browser input box and defines it the color Variable 
     color = Browser.inputBox("Enter the color"); 

     for (i=0;i<spreadsheetIDs.length;++i) { 
     thisID=spreadsheetIDs[i]; 
    //This opens the first ID, and opens up the Sheet1 sheet. ALL of the sheets in the territory MUST have this name. 
     sheet=SpreadsheetApp.openById(thisID).getSheetByName('Sheet1'); 

    //This loops through the column 1 and 13 and checks for the correct font size all the way up to row 59. 
    //When it finds the correct fontsize, it sets the fontcolor to the color variable value. 
    for (e=1;e<58;++e) { 
     //Row A 
     c=1 
      if(sheet.getRange(e,c).getFontSize() == '24') { 
       sheet.getRange(e,c).setFontColor(color);} 
     //Row M 
     c=13 
      if(sheet.getRange(e,c).getFontSize() == '24') { 
       sheet.getRange(e,c).setFontColor(color);} 
      } 
    //Set Background for A1 and Color for D1 
    sheet.getRange(1,1).setBackground(color); 
    sheet.getRange(1,4).setFontColor(color); 
     } 
     } 

回答

1

我覺得這個Best Practices for App Script可以幫到你。

使用批處理操作

您可以編寫腳本來採取的最大優勢內置 緩存,通過減少數量的讀取和寫入。讀取和寫入命令速度較慢,交替使用 。要加速腳本,請使用一個命令將所有數據 讀入數組,並對數組執行任何操作,並使用一個命令將數據寫出。

下面是一個示例 - 您不應該遵循或使用的示例。在畫廊 電子表格分形藝術腳本(僅在 舊版本的谷歌表可用)使用下面的代碼來設置每一個細胞的 背景色的100×100的電子表格網格:

// DO NOT USE THIS CODE. It is an example of SLOW, INEFFICIENT code. 
// FOR DEMONSTRATION ONLY 
var cell = sheet.getRange('a1'); 
for (var y = 0; y < 100; y++) { 
    xcoord = xmin; 
    for (var x = 0; x < 100; x++) { 
    var c = getColor_(xcoord, ycoord); 
    cell.offset(y, x).setBackgroundColor(c); 
    xcoord += xincrement; 
    } 
    ycoord -= yincrement; 
    SpreadsheetApp.flush(); 
} 

該腳本效率低下:循環遍歷100行100列, 連續寫入10,000個單元格。 Google Apps腳本 寫回緩存有幫助,因爲它會強制在每行的末尾使用刷新 進行回寫。由於緩存,調用電子表格僅有100 。

但是,通過批處理調用,代碼可以變得更高效。 下面是在該單元格範圍被讀入稱爲 顏色數組中,對 陣列中的數據進行顏色賦值操作,並且陣列中的值被寫入到電子表格中的重寫:

// OKAY TO USE THIS EXAMPLE or code based on it. 
var cell = sheet.getRange('a1'); 
var colors = new Array(100); 
for (var y = 0; y < 100; y++) { 
    xcoord = xmin; 
    colors[y] = new Array(100); 
    for (var x = 0; x < 100; x++) { 
    colors[y][x] = getColor_(xcoord, ycoord); 
    xcoord += xincrement; 
    } 
    ycoord -= yincrement; 
} 
sheet.getRange(1, 1, 100, 100).setBackgroundColors(colors); 
0

這確實是最慢的方法!

您需要做的是將所有值讀入數組,更改值,然後一次寫回。

因此,請檢查Range類中的getValues和getFontColors以及setValues和setFontColors等調用,這將允許您僅向Range對象創建兩個調用,並大量減少執行時間。

+0

當我讀取數組中的值之後,我如何定位所述值以更改我想要的特定值?我需要特別改變FontSize爲X的字體。所以,在將所有這些FontSizes寫回到我的數組後,我如何知道它們來自哪裏以將它們作爲FontColor改變的目標? –

+0

我做了它使用2嵌套循環與他們檢查單元格值的if語句。使用parseint(我不知道爲什麼它在一個字符串值),然後加1到它的單元格值。 (因爲循環從0開始,谷歌表格從1開始。)完美地工作! –

+0

很酷。對不起,延遲迴復。 所以我會讀取整個範圍的兩個數組的大小和值,使用getFontSizes()和getValues(),然後使用嵌套循環遍歷該數組,並更改值數組中的相應值 - 有道理? 聽起來像這就是你做的! – alfiethecoder