电子表格到Google doc表很长(4分钟)

时间:2018-06-14 14:37:03

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

请帮忙!它是一个项目,因此通过在第二列中搜索' o' (oui in French / yes English)意味着该项目将在2019年,所以我将从我的Google电子表格中获取我想要的几个列(名称项目,日期,主厨项目......)的信息(每次我找到“我将创建一个Google Doc文件”,然后使用带有8行和2列的makecopy()模板将此信息放入已存在的Google Doc表中,以便将所有信息放入第二列。事实上,我的代码非常慢,如果你有任何想法可以实现,那将是很好的,因为它可以在大约4分钟内完成工作



function create_Google_Docs_2019_0() {
      
      var sheet = SpreadsheetApp.openById(SPREADSHEET_ID).getSheetByName(SHEET_NAME);
      var numRows = sheet.getLastRow();
      var lastColumn = sheet.getLastColumn();
      var data = sheet.getRange(1,1,numRows,lastColumn).getValues()
    
      
      var targetFolder = DriveApp.getFolderById(FOLDER_ID);
      Logger.log('targetFolder name: ' + targetFolder.getName());
      
 
      var nombre_projets_2019 = 0 ;
      
      
      // the second row because the first is not important
       for(n=1;n < data.length;n++) {
        
        //verify if we have 'o' so we will create the corresponded document
         if ( data[n][1] == 'o'){
          nombre_projets_2019 = nombre_projets_2019  + 1;
          var name_projet = data[n][2];
           
          //var name_document = 'Projet ' + nom_projet ;
          var name_document = name_projet ;
          /** Search in the folder if we already have the file**/
          
          if( checkFile_in_a_Folder(name_document,targetFolder) == 1){
            //yeys we will update it later
            Logger.log('On va réecrire le ficher / overwrite le ficher ');
          } 
          else
          {//we will create it 
            Logger.log('On va le créer avec les données qu on a dans le tableau ');
            //Make a copy of the template file already exists in the file
            var documentId = DriveApp.getFileById(TEMPLATE_DOC_ID).makeCopy().getId();
            
            //Rename the copied file
            var name = DriveApp.getFileById(documentId).setName(name_document);
            var body = DocumentApp.openById(documentId).getBody();
            if(body)
            {
              var ok = 0;       //for instance we don't hava a table
              var numChildren=body.getNumChildren();
              var i=0;
              //we will loop
              while(ok ==0 && i<numChildren)
              {
                var child=body.getChild(i);
                /** =So we are concerned by the first table with 8 or more rows=========**/
                Logger.log('Le type dans la boucle  ' + child.getType());
                //if it's a table
                if(child.getType()==DocumentApp.ElementType.TABLE && child.asTable().getNumRows() >= 8)
                {
                  //we found what we've searched
                  ok=1;   
                  
                  
  /** on va inserer les informations dans le tableau**/
                  
                  var k = 1;
                  child.asTable().getCell(0, k).editAsText().setText( data[n][colonne_nom_de_projet-1]);
                  child.asTable().getCell(1, k).editAsText().setText( data[n][colonne_code_de_projet-1] )  ;
                  child.asTable().getCell(2, k).editAsText().setText( data[n][colonne_chef_de_projet-1])  ;
                  child.asTable().getCell(3, k).editAsText().setText( data[n][colonne_service_pilote_de_projet-1] )  ;
                  child.asTable().getCell(4, k).editAsText().setText( data[n][colonne_autres_services_projet-1] )  ;
                  child.asTable().getCell(5, k).editAsText().setText( data[n][colonne_typede_projet-1] )  ;
                  child.asTable().getCell(6, k).editAsText().setText( data[n][colonne_perimetre__projet-1] )  ;
                  child.asTable().getCell(7, k).editAsText().setText( data[n][colonne_date_de_projet-1] )  ;
                }
                i++;
              }
            }
          } 
        }
        Logger.log('Nombre de projets 2019 ' +  nombre_projets_2019 );
      }
    }
&#13;
&#13;
&#13;

我没有在Google文档中创建表格我从模板中获取表格所以我搜索了第一个包含8行或更多行的子项,并将值放在右边,大问题也来自表单谢谢

也是我在代码中第一个声明的常量

var colonne_nom_de_projet = 3
var colonne_code_de_projet = 1
var colonne_chef_de_projet = 7
var colonne_service_pilote_de_projet = 8
var colonne_autres_services_projet = 11
var colonne_typede_projet = 10
var colonne_perimetre__projet  = 12
var colonne_date_de_projet = 18

修改编辑:我的印象是我的帖子不是那么清楚,所以在图片中解释这个^^^^在这个谷歌电子表格我有很多列和as here和许多行79和我搜索第二col oum用于&#39; o&#39;意味着它将是2019年的一个项目,所以在另一个文件夹中有一个Google文档模板 here,我有一个第一个表(8行),用于将信息形成一个&#39; o&#39;我感兴趣的具有特定列的行(所以我将这些列声明为变量)所以每次我复制模板并使用第3列中的名称项目重命名它然后放置值,这样我将有名为b,c等的文件

0 个答案:

没有答案
相关问题