交互式Google Spreadsheet至Google Doc编辑不起作用

时间:2018-07-03 10:24:15

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

我有几个项目,它们遵循相同的结构,它们有一个8行,两列的表,到目前为止,我编写了一个脚本,该脚本将所有文档都放入表格中,然后将其放入信息作为列的表格中。一个容器绑定的脚本,我想被多个用户使用。我被封锁了很长时间,因为如果要在表中进行修改,我希望进行交互,它将在Google Doc中进行修改,反之亦然。我开始尝试如果我修改日期,请在列日期上输入日期,然后单击Google Doc的网址以查看更改,但它不起作用。这是我的代码:

编辑这是我新修改的代码:

function modify_Date_Google_Spreadsheet_to_Google_Doc_Project(e) {
  
  find_columns_in_projet();
  Logger.log(">> The column URL >> " + COLUMN_URL );
  Logger.log("The column date where we will modify it " +  column_date_project);
  
  var tss = SpreadsheetApp.openById(SPREADSHEET_ID);
  var sheet = tss.getSheets()[0];
  var numRows = sheet.getLastRow();
  var lastColumn = sheet.getLastColumn();
  
  //from the second line car the first line we have the headers
  var data = sheet.getRange(1,1,numRows,lastColumn).getDisplayValues();
  
  if ( ( e.range.getColumnIndex() == column_date_project )  )
  {
    var activeRow = SpreadsheetApp.getActiveRange().getRowIndex();
    var URL = e.range.offset(0,1,1,1).getValue();
    Logger.log('The URL is : ' + URL );
    var body = DocumentApp.openByUrl(URL).getBody();
    Logger.log('The body is ' + body );
    if(body)
    {
      var ok = 0;                                    //for the moment we don't have the table to modify the values we've put in the spreadsheet 
      var numChildren=body.getNumChildren();
      var compteur=0;
      //while we don't find the table we will search
      while(ok ==0 && compteur<numChildren)
      {
        var child=body.getChild(compteur);
        /** =========We are concerned by the first table with at least 8 rows ===**/
        Logger.log('the type in the loop  ' + child.getType());
        
        //here is our table  **/
        if(child.getType()==DocumentApp.ElementType.TABLE && child.asTable().getNumRows() >= 8)
        {
          //so the variable gets 1 >>  ok = 1
          ok=1;   
          
          /**  The number of rows in the Google Doc table **/
          var numrows = child.asTable().getNumRows();
          Logger.log('The number of rows is   ' + numrows);
          //Logger.log('The new date is ' + data[activeRow][colonne_date_de_projet-1]);
          /** we will loop in the table **/ 
          
          var k = 1;     //we know the information is at right so we don't loop we will replace the value 
          /**  is not working   **********************************************/
          //child.asTable().getCell(7, k).editAsText().setText( data[activeRow][column_date_project-1] )  ;
         
          
          /**** is working   ***/
          child.asTable().getCell(7, k).editAsText().setText( 10 )  ;
        }
        compteur++;       /** until we find our table **/
      }
    }
  }
}

这是编辑触发器,因为我在项目中还有另一个。这是Google Spreadsheed,其中包含脚本:https://docs.google.com/spreadsheets/d/1k_kj98U__0Bk44gh0qRFpaVx0ru3sN1pSPGiMQwimxo/edit?usp=sharing,我的Google Doc项目文件夹位于:https://drive.google.com/drive/folders/1x1m7tqfoSY6yW5gvwoIoh9jRPuiqwADO?usp=sharing太好了:)非常感谢enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

我尝试了另一个代码,该代码仅在我修改了该代码中第四个项目的日期时才起作用,而我仅替换了

  

data_sheet [activeRow] [column_date_project-1]与   data_sheet [4] [column_date_project-1],因为未定义activeRow   因此,在我编辑与数据对应的单元格时恢复活动单元格行的任何想法都很棒。   

/** working with 4 as the 4 row's date modifies****/
function works(e) {
  
  find_columns_in_projet();
  Logger.log(">> The column URL >> " + COLUMN_URL );
  Logger.log("The column date where we will modify it " +  column_date_project);
  
  var tss_bis = SpreadsheetApp.openById(SPREADSHEET_ID);
  var sheet_bis = tss_bis.getSheets()[0];
  var numRows_bis = sheet_bis.getLastRow();
  var lastColumn_bis = sheet_bis.getLastColumn();
  
  //from the second line car the first line we have the headers
  var data_sheet = sheet_bis.getRange(1,1,numRows_bis,lastColumn_bis).getDisplayValues();
  
  if ( ( e.range.getColumnIndex() == column_date_project )  )
  {
    var activeRow = SpreadsheetApp.getActiveRange().getRowIndex();
    
    Logger.log('The active row  is : ' + activeRow );
    var URL = e.range.offset(0,1,1,1).getValue();
    Logger.log('The URL is : ' + URL );
    var body = DocumentApp.openByUrl(URL).getBody();
    Logger.log('The body is ' + body );
    if(body)
    {
      var ok = 0;                                    //for the moment we don't have the table to modify the values we've put in the spreadsheet 
      var numChildren=body.getNumChildren();
      var compteur=0;
      //while we don't find the table we will search
      while(ok ==0 && compteur<numChildren)
      {
        var child=body.getChild(compteur);
        /** =========We are concerned by the first table with at least 8 rows ===**/
        Logger.log('the type in the loop  ' + child.getType());
        
        //here is our table  **/
        if(child.getType()==DocumentApp.ElementType.TABLE && child.asTable().getNumRows() >= 8)
        {
          //so the variable gets 1 >>  ok = 1
          ok=1;   
          
          /**  The number of rows in the Google Doc table **/
          var numrows = child.asTable().getNumRows();
          Logger.log('The number of rows is   ' + numrows);
          Logger.log('The new date is ' + data_sheet[4][column_date_project-1]);
         
          /** we will loop in the table **/ 
          
          var k = 1;     //we know the information is at right so we don't loop we will replace the value 
          /**  is working   **********************************************/
          child.asTable().getCell(7, k).editAsText().setText( data_sheet[4][column_date_project-1] )  ;
        }
        compteur++;       /** until we find our table **/
      }
    }
  }
}