先前的文章提到

透過Query語法搭配AlternateColor() 這段語法

就可以做到初步做到篩選資料,美化的功能

這次,試試看不一樣的方式

Query語法的好處是方便跟快速

但是遇到這種有合併儲存格的結構

就顯得有點尷尬

畢竟,它只負責資料面的問題

結構面就必須要想個方法來處理

找了一下官方文件

這段就是用來複製結構使用

規定了,從哪一個Column跟Row的起訖

照著改寫,第一階段就完成,把結構複製到目的Sheet

copyFormatToRange(sheet, column, columnEnd, row, rowEnd)

Copy the formatting of the range to the given location. Copy the formatting of the range to the given location. If the destination is larger or smaller than the source range then the source will be repeated or truncated accordingly. Note that this method is formatting only.
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var source = ss.getSheets()[0];
 var destination = ss.getSheets()[1];

 var range = source.getRange("B2:D4");

 // This copies the data in B2:D4 in the source sheet to
 // D4:F6 in the second sheet
 range.copyFormatToRange(destination, 4, 6, 4, 6);

接著,進到轉資料部分

由於資料面,要改用程式部分轉換

因此,除了必須要把複製資料的語法寫進去外,還需要同時指定Where條件

原先,Where  條件有兩個

Where F like ‘%Chieh%’ and N=’Open'”

指定F(負責人)這欄位有出現Cheih,或者是N(狀態)這欄位尚未被完成Open

N這欄位沒事,因為通常都是需要先檢視,尚未被結束的工作項目

但是F這欄位就很有問題

部門不一定都是同樣的負責人在處理工作項目

套句工程師的語言

它必須要被設定成變數!!

變數的來源,不外乎是用參數傳遞(這邊是Google Doc勒!!)或是人為輸入

這邊,似乎用第2個方式比較可行且方便使用

官方提供了inputBox這個方法,就可以做出這個效果

透過提供一個Pops up視窗,讓使用者可以輸入一些資料

因為回傳的是字串,所以只要把設定一個變數來接這回傳值即可!

inputBox(title, prompt, buttons)

Pops up a dialog box with a text input box in the user’s browser. The inputBox method raises a client side input box with the given title, that displays the given prompt to the user, and offers a choice of buttons to be displayed. Note that this function causes the server-side script to be suspended. It will resume automatically after the user clears the dialog, but JDBC connections will not persist across the suspension.
 // The code below will set the value of name to the name input by the user, or 'cancel'
 var name = Browser.inputBox('ID Check', 'Enter your name', Browser.Buttons.OK_CANCEL);
 

Parameters

Name Type Description
title String the title for the dialog box
prompt String the text to be displayed in the dialog box
buttons ButtonSet an enum from Browser.Buttons

Return

String — text entered by the user (or ‘cancel’ for a canceled or dismissed dialog)
剩下,就只要把一些條件拼湊起來

指定這個負責人的資料才秀出來就好

function copyEachDataByManager() {
 var name = Browser.inputBox(‘Enter your name plz,BTW your data will be reseted’, Browser.Buttons.OK);
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var source = ss.getSheetByName(“部門工作清單”);
 var destination = ss.getSheetByName(name);
 var totalRows = SpreadsheetApp.getActiveRange().getNumRows();
 var totalColumns = 20;//SpreadsheetApp.getActiveRange().getNumColumns();

 var range = source.getRange(“A1:Z4”);j

 // This copies the data in B2:D4 in the source sheet to
 // D4:F6 in the second sheet
 range.copyFormatToRange(destination, 1, 4, 1, 4);
 range.copyValuesToRange(destination, 1, 4, 1, 4);

  var data = source.getRange(4,1, source.getLastRow(), source.getLastColumn()).getValues();

  var dest = [];
  for (var i = 0; i < data.length; i++ ) {
    if (data[i][5].toString().indexOf(name)!=-1 && data[i][13].toString().indexOf(“Open”)!=-1) {
      dest.push(data[i]);
    }
    //Logger.log(data)
    if (dest.length > 0 ) {
    destination.getRange(4,1,dest.length,dest[0].length).setValues(dest);
    }
  }
  //alternateColor();
  /*
 這邊記得,如果要用剛上篇文章的程式碼,記得把Header判斷改為兩行
 /*
}

Leave a Reply

Your email address will not be published. Required fields are marked *