Ovewrite表Google Script交互式Google表格到Google Docs,反之亦然

时间:2018-07-12 21:03:24

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

这是我在查看模式下的最终项目:

https://drive.google.com/open?id=1gOOb3hND3q2v0vTy8SfPVHFiCnTkJPR5

每行对应一个Google Doc项目,因此当我在该行中进行修改时,它将修改相应的Doc;当我在修改后单击同一窗口中的文档以查看该文档时,如果单击“提交”按钮,我将更改该行所以我有一个100%的交互性,只有触发器onEdit(e)会调用函数Edit(e)和openDialog(e)。

问题在于它可以覆盖图像中的Google Doc文件。问题来自功能

onEdit(e){
  Edit(e);
  openDialog(e); // tick to see the project in the same window and modify it transfer to the row 
}

如果我放openDialog(e);在注释中,例如,如果我删除行中的文本字段,它将正确地对其进行修改,并使用openDialog(e)将其覆盖。我已尝试像在New.gs文件中一样将openDialog代码放入Edit(e)代码中,但是它不成功,请关闭文档https://script.google.com/d/1bV_eJONvUAbHyO6OB04atfm_sb5ZO8LWNoQ23fxf0lFnRzHRSwW7hsQc/edit?usp=sharing,您有解决的办法吗?非常感谢您:) 它们分别运行良好。在一起,否可以覆盖文件,所以我不知道我是否需要关闭Google Doc,因为我已经打开了2次了?

这是列J;我设置了一个触发器以在同一窗口中查看Google Doc,并且可以在内部提交按钮中进行修改并更改行(这是openDialog的对应部分) enter image description here 编辑:对不起,这是代码

var TITLE = 'Show Google Doc';
var SPREADSHEET_ID = "17ssKkCAoPUbqtT2CACamMQGyXSTkIANnK5CjbbZ1LZg"; // = assign your spreadsheet id to this variable


var column_name_project ;
var column_code_project ;
var column_chef_project;
var column_service ;
var column_other_services ;
var column_type_of_project ;
var column_perimeter ; 
var column_date_project ;

var COLUMN_URL ;
var COLUMN_VIEW_Google_Doc;


/** will return the number of the column correspondant **/
function find_columns_in_projet(){
 //search the columns
}

function onEdit(e){
  Edit(e);
 // openDialog(e);
}


function Edit(e) { 
  find_columns_in_projet();

  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();
  //Access the range with your parameter e.
  var range = e.range;
  
  var row = range.getRow();
  var column = range.getColumn();
  if( e.range.getColumnIndex() != COLUMN_URL + 1 ) {
    var URL = data_sheet[row-1][COLUMN_URL-1];
    Logger.log('Le URL est bien : ' , URL);
    
    var body = DocumentApp.openByUrl(URL).getBody();
    Logger.log('The body is ' + body );
    if(body)
    {... code to write the information from the spreadsheet to the Google Doc 
   }   
}
}

/** every row in the current Sheet corresponds to a Google Doc Document   **/
/**  to see the Google Doc in the same page  click in colum J and be able to modify the Google Doc 8 rows table inside
By clicking the button Submit it will transfer the information with what you have changed to the row of the corresponding project int the Sheet  **/

function openDialog(e) {
  
  /**  columns in the Spreadsheet that are lines in the Google Doc table  **/
/**  find_columns_in_projet();
  
  /** the good Sheet  **/
  if( ( e.range.getSheet().getName() === "Sheet1" ) &&   ( e.range.getColumnIndex() == COLUMN_VIEW_Google_Doc ) ) {
    if( e.value === "TRUE" ) {
      try {
        //Get Google Doc body
        /**  the URL that is in the column I  **/
        
        var URL = e.range.offset(0,-1,1,1).getValue();
        e.range.setValue("FALSE");
        
        
        // Add this line
        var ui = HtmlService.createTemplateFromFile('ModeLessDialog');
        ui.body = URL;                                                                         // Pass url to template
        ui.insert = e.range.getRow() ;
        ui = ui.evaluate().setWidth(1000).setHeight(500);
        SpreadsheetApp.getUi().showModalDialog(ui, 'Show Google Doc');
      }
      catch(err) {
        Logger.log(err);
      }
    }
  }
}


function submitDoc(url,insert) {
  /** the Spreadsheet need for getRange insert position **/
  var tss_bis = SpreadsheetApp.getActiveSpreadsheet();
  var sheet_bis = tss_bis.getSheets()[0];
  find_columns_in_projet();
  try {
    Logger.log(url);
    var google_doc = DocumentApp.openByUrl(url) ;
    var body = google_doc.getBody();
    if(body) {
code to write the information from the Google Doc into the Spreadsheet by button submit
      }
      Logger.log(body);
    }
    return true;
  }
  catch(err) {
    Logger.log(err);
  }
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<iframe id="srcFrame" src="<?= body ?>" name="<?= insert ?>" width="1000" height="400"></iframe>
<input type="button" value="Sumit changes to the Spreadsheet" onclick="submitDoc()">
<script>
function submitDoc() {
var url = document.getElementById("srcFrame").src;
var insert = document.getElementById("srcFrame").name;
google.script.run.submitDoc(url,insert);
google.script.host.close();
}
</script>
</body>
</html>

0 个答案:

没有答案